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

时间:2023-01-22 20:59:36

I want to achieve this following route poly line view over Apple map.如何在Apple地图上绘制两个注释之间的路线图?

我想在Apple地图上实现以下路线多线视图。

And want to show poly line on road, which connect from source to destination.

并希望在道路上显示从源到目的地连接的多边形线。

my code for viewcontroller header file is...

我的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;

my code for viewcontroller implementation file is...

我的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];
}

and my out is...如何在Apple地图上绘制两个注释之间的路线图?

而我的出局是......

Please help me... Thanks in advance.

请帮帮我...先谢谢。

1 个解决方案

#1


1  

Your location array should not be filled with only two coordinates. Because adding only two coordinates will join the two points together in a straight line. So you need to add more coordinates to your location array in order to have a more accurate polyline.

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

#1


1  

Your location array should not be filled with only two coordinates. Because adding only two coordinates will join the two points together in a straight line. So you need to add more coordinates to your location array in order to have a more accurate polyline.

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