iOS MapKit连接道路地图上的多个点

iOS MapKit连接道路地图上的多个点,ios,mapkit,overlay,polyline,directions,Ios,Mapkit,Overlay,Polyline,Directions,我正在使用以下代码将接点(地图上的点)与多段线连接: CLLocationCoordinate2D coordinates[allLocations.count]; int i = 0; for (locHolder *location in allLocations) { coordinates[i] = CLLocationCoordinate2DMake([location.lat floatValue], [location floatValue]);

我正在使用以下代码将接点(地图上的点)与多段线连接:

CLLocationCoordinate2D coordinates[allLocations.count];
    int i = 0;
    for (locHolder *location in allLocations) {
        coordinates[i] = CLLocationCoordinate2DMake([location.lat floatValue], [location floatValue]);
        i++;
    }
    MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coordinates count:[allLocations count]];
    self->polyline = polyline;
    [self.mapView addOverlay:self->polyline level:MKOverlayLevelAboveRoads];
但是此代码通过空中连接它们(忽略道路),是否可以将多个CLLOCATIONCoordinated2D位置与道路后面的多段线连接

** 还有一个小问题, [allLocations count]和allLocations.count之间的性能是否有任何差异


谢谢。

这些天来,我遇到了同样的问题,到处寻找答案,我发现这个案例的答案非常有用

首先,假设您的MapViewController中有一个包含起点和终点的对象数组,每个对象的类型都是
CLLocationCoordinate2D

在MapViewController.h中

然后,在实现中,使用以下数据填充该数组:

现在您已经有了起点和终点的数组,可以开始在它们之间绘制路线(从a到B,从B到C,从C到D)

添加一个按钮,然后连接一个
iAction
,这样您就可以使用该方法执行反魔法

-(iAction)btnDirectionsPressed:(id)发送方{
[self-enableUI:NO];//此方法启用或禁用与用户交互的所有UI元素
dispatch\u semaphore\u t semaphore=dispatch\u semaphore\u create(0);//创建信号量
__block NSMutableArray*routes=[[NSMutableArray alloc]init];//用于存储MKRoute对象和MKAnnotationPoint对象的数组,以便我们可以在地图上绘制它们
__块NSMutableArray*注释=[[NSMutableArray alloc]init];
调度异步(调度获取全局队列(调度队列优先级后台,0)^{
用于(RouteObject*位于_位置的RouteObject){
MKDirectionsRequest*directionsRequest=[MKDirectionsRequest新建];
[directionsRequest setTransportType:MKDirectionsTransportTypeAutomobile];
[DirectionRequest setSource:[routeObject originMapItem]];
[DirectionRequest setDestination:[routeObject destinationMapItem]];
[DirectionRequest SetRequestSalterRoutes:否];
MKDirections*direction=[[MKDirections alloc]initWithRequest:directionsRequest];
//对于locations数组中的每个对象,我们从其原点和目的地请求该路由
[方向计算方向WithCompletionHandler:^(MKDirectionsResponse*响应,NSError*错误){
如果(错误){
NSLog(@“获取您的方向时出错”);
返回;
}
MKRoute*route=[response.routes firstObject];
[路由添加对象:路由];
[annotations addObject:[routeObject destinationAnnotation];
dispatch_semaphore_signal(信号量);//发送一个信号量准备使用的信号
}];
}
});
调度异步(调度获取全局队列(调度队列优先级后台,0)^{
对于(int i=0;i<\u locations.count;i++){
dispatch_semaphore_wait(semaphore,dispatch_TIME_FOREVER);//等待一个信号量准备使用
dispatch_async(dispatch_get_main_queue(),^{//对于每个元素,将其分派到主队列以绘制对应于该位置的路由和注释
MKRoute*route=routes[i];
[self.mapView addOverlay:route.polyline];
[self.mapView addAnnotation:annotations[i]];
});
}
dispatch_async(dispatch_get_main_queue(),^{//最后,分派到主队列启用元素并调整映射大小以显示所有注释
[自启用用户界面:是];
[自订路线];
});
});
}
希望这有帮助


Joel。

使用MKDirectionsRequest获取位置之间的驾驶路线。有关示例,请参见。另外,
[allLocations count]
allLocations.count
完全相同(请参阅)。我已经查看了MKDirectionsRequest,但它允许连接点A和B,但我需要的是通过B、C、D、E连接点A和X。。。这甚至可以实现吗?称之为A到B,然后是B到C,然后…嗨,我在多年的差距后正在开发iOS。尝试运行此代码时,出现错误为“使用未声明的identifire RouteObject”。我已经包括了Mapkit和CoreLocation框架并导入到文件中。请建议。我想在起点和终点之外的多个点之间创建一个“查找距离”。@Pawriwes RouteObject是我为此而制作的一些POJO。它有两个属性,都是CLLocationCoordinate2D类型,还有一些有用的方法。
@property (nonatomic, strong) NSArray *locations;
_locations = [NSArray arrayWithObjects:
    [RouteObject routeWithOrigin:CLLocationCoordinate2DMake(-23.595571, -46.684408) destination:CLLocationCoordinate2DMake(-23.597886, -46.673950)],
    [RouteObject routeWithOrigin:CLLocationCoordinate2DMake(-23.597886, -46.673950) destination:CLLocationCoordinate2DMake(-23.597591, -46.666805)],
    [RouteObject routeWithOrigin:CLLocationCoordinate2DMake(-23.597591, -46.666805) destination:CLLocationCoordinate2DMake(-23.604061, -46.662728)], nil];