如何在Apple地图上绘制两个注释之间的路线图?

时间:2018-03-29 13:56:14

标签: ios objective-c xcode mkmapview mkpolyline

我想在Apple地图上实现以下路线多线视图。I want this type of route polyLine

并希望在道路上显示聚合线,从源到目的地连接。

我的viewcontroller头文件代码是......

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface TrackViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@property (strong,nonatomic) NSMutableArray *arrAnnotation;
@property (nonatomic, retain) MKPolyline *polyLine;
@property (nonatomic, retain) MKPolylineView *polyLineView;

@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLLocation *currentLocation;

我的viewcontroller实现文件代码是......

- (void)viewDidLoad {
[super viewDidLoad];

_mapView.showsUserLocation = YES;

if ([CLLocationManager locationServicesEnabled]) {
    if (self.locationManager == nil) {
        self.locationManager = [[CLLocationManager alloc]init];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.distanceFilter = kCLDistanceFilterNone;
    }
    [self.locationManager startUpdatingLocation];
}

NSArray *name=[[NSArray alloc]initWithObjects:
               @"Mumbai",
               @"Chennai", nil];
self.arrAnnotation=[[NSMutableArray alloc]initWithCapacity:name.count];

MKPointAnnotation *mappin1, *mappin2;

CLLocationCoordinate2D location[3];

mappin1 = [[MKPointAnnotation alloc]init];
location[0] = CLLocationCoordinate2DMake(19.129275,72.905273);
mappin1.coordinate=location[0];
mappin1.title=[name objectAtIndex:0];
[self.arrAnnotation addObject:mappin1];

mappin2 = [[MKPointAnnotation alloc]init];
location[1] = CLLocationCoordinate2DMake(13.063426,80.288086);
mappin2.coordinate=location[1];
mappin2.title=[name objectAtIndex:1];
[self.arrAnnotation addObject:mappin2];

[self.mapView addAnnotations:self.arrAnnotation];
self.mapView.mapType = MKMapTypeStandard;
self.mapView.showsUserLocation = YES;

self.polyLine = [MKPolyline polylineWithCoordinates:location count:2];
[self.mapView setVisibleMapRect:[self.polyLine boundingMapRect]];
[self.mapView addOverlay:self.polyLine];
}

我的出局是...... this is my output

请帮帮我... 提前谢谢。

1 个答案:

答案 0 :(得分:1)

您的位置数组不应仅使用两个坐标填充。因为仅添加两个坐标会将两个点以直线连接在一起。因此,您需要为位置数组添加更多坐标,以便获得更准确的折线。