3

我对Objective-c很陌生。

在我的应用程序中,我试图将用户采取的路线绘制到地图上。

到目前为止,这是我仅获取用户当前位置的内容:

#import "StartCycleViewController.h"
#import "CrumbPath.h"


@interface StartCycleViewController ()

@property (nonatomic, strong) CLLocationManager *locationManager;

@property (nonatomic, strong) IBOutlet MKMapView *map;

@property (nonatomic, strong) UIView *containerView;





@end

@implementation StartCycleViewController

@synthesize cycleLocation = _cycleLocation;
@synthesize currentCycleLocation = _currentCycleLocation;






 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
 }

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self startCycleLocation];

    _containerView = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.containerView];

    [self.containerView addSubview:self.map];
    // Do any additional setup after loading the view.
}


- (void)dealloc
{
    self.locationManager.delegate = nil;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - startCycleLocation

- (void)startCycleLocation{

    if (!_cycleLocation){
        _cycleLocation = [[CLLocationManager alloc]init];
        _cycleLocation.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
        _cycleLocation.distanceFilter = 10;
        _cycleLocation.delegate = self;

    }
    [_cycleLocation startUpdatingLocation];

}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation     *)newLocation fromLocation:(CLLocation *)oldLocation {

    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
    self.longitudeLabel.text = [NSString stringWithFormat:@"%.8f",    currentLocation.coordinate.longitude];
    self.latitudeLabel.text = [NSString stringWithFormat:@"%.8f",    currentLocation.coordinate.latitude];

    }
}



- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"%@",error);

    if ( [error code] != kCLErrorLocationUnknown ){
        [self stopLocationManager];
    }
}

- (void) stopLocationManager {
    [self.cycleLocation stopUpdatingLocation];
}



@end

我在网上浏览了一下,收集了我应该使用的信息MKPolyline并给它坐标。但我只是不确定如何存储位置,然后MKPolyline在应用程序运行时连续发送它们用于绘制点。

4

1 回答 1

4

您应该只创建一个NSMutableArray来保存您在其中实例化的位置viewDidLoad。所以有didUpdateToLocation(或者,如果支持 iOS 6 及更高版本,您应该使用didUpdateToLocations)只需将位置添加到数组,然后MKPolyline从该数组构建一个,将其添加MKPolyline到地图,然后删除旧的MKPolyline. 或者您可以将所有线段添加为单独的MKPolyline对象,但想法是相同的,创建一个模型来保存您的位置(例如 a NSMutableArray),然后将适当的MKPolyline对象添加到地图视图中。

例如,您可以执行以下操作:

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *location = [locations lastObject];

    if (location.horizontalAccuracy < 0)
        return;

    [self.locations addObject:location];
    NSUInteger count = [self.locations count];

    if (count > 1) {
        CLLocationCoordinate2D coordinates[count];
        for (NSInteger i = 0; i < count; i++) {
            coordinates[i] = [(CLLocation *)self.locations[i] coordinate];
        }

        MKPolyline *oldPolyline = self.polyline;
        self.polyline = [MKPolyline polylineWithCoordinates:coordinates count:count];
        [self.mapView addOverlay:self.polyline];
        if (oldPolyline)
            [self.mapView removeOverlay:oldPolyline];
    }
}

并且记得指定地图是如何绘制的MKPolyline。因此,将您的视图控制器设置delegate为您的MKMapView,然后您可以执行以下操作:

#pragma mark - MKMapViewDelegate

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolyline class]])
    {
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];

        renderer.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
        renderer.lineWidth   = 3;

        return renderer;
    }

    return nil;
}

// for iOS versions prior to 7; see `rendererForOverlay` for iOS7 and later

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolyline class]])
    {
        MKPolylineView *overlayView = [[MKPolylineView alloc] initWithPolyline:overlay];

        overlayView.strokeColor     = [[UIColor blueColor] colorWithAlphaComponent:0.7];
        overlayView.lineWidth       = 3;

        return overlayView;
    }

    return nil;
}
于 2014-02-01T14:14:12.657 回答