使用MKPolyline在地图上绘制用户路线 [英] Plotting a users route taken on a map using MKPolyline

查看:90
本文介绍了使用MKPolyline在地图上绘制用户路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Objective-c还是很陌生.

I'm quite new to Objective-c.

在我的应用中,我试图将沿路线行驶的用户绘制到地图上.

In my app, I'm trying to plot the users taken route onto a map.

这是我到目前为止获得的最新信息:

Here is what I have so far that just gets the users current location:

#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连续发送这些点以绘制点.

I've had a look around online and gather that I should use MKPolylineand give it coordinates. But I'm just not sure how I would store the locations and then send them use MKPolyline to plot the points, continuously while the app is running.

推荐答案

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

You should just create a NSMutableArray to hold your locations that you instantiate in viewDidLoad. So have didUpdateToLocation (or, if supporting iOS 6 and higher, you should use didUpdateToLocations) simply add a location to an array, then build a MKPolyline from that array, add that MKPolyline to the map, and then remove the old MKPolyline. Or you could add all of the line segments as individual MKPolyline objects, but the idea is the same, create a model to hold you locations (e.g. a NSMutableArray) and then add the appropriate MKPolyline object(s) to the map view.

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

For example, you could do something like:

#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的方式.因此,将视图控制器设置为MKMapViewdelegate,然后您可以执行以下操作:

And remember to specify how the map is to draw the MKPolyline. So set your view controller to be a the delegate of your MKMapView, and you can then do something like the following:

#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;
}

这篇关于使用MKPolyline在地图上绘制用户路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆