使用MKPolyline绘制在地图上拍摄的用户路线

时间:2014-02-01 12:56:37

标签: objective-c location mapkit core-location mkpolyline

我对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使用{{1}}连续绘制点。

2 个答案:

答案 0 :(得分:3)

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

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

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的{​​{1}},然后您可以执行以下操作:

delegate

答案 1 :(得分:0)

您的要求需要详细解释,这就是我为您提供链接的原因,这对他的这类问题有很大帮助。 draw line on map

如果你需要什么告诉我。