我正在使用此代码将图钉( map 上的点)与折线连接起来:
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];
但是这段代码通过无线方式将它们连接起来(忽略道路),是否可以将多个 CLLocationCoordinate2D 位置与沿着道路的多段线连接起来?
** 还有一个简短的子问题, [allLocations count] 和 allLocations.count 在性能上有什么不同吗?
谢谢。
最佳答案
这几天我遇到了同样的问题,到处寻找答案,我找到了this来自 Rob 的回答对于这种情况非常有用。
首先,假设您的 MapViewController 中有一个包含起点和终点的对象数组,每个对象的类型都是 CLLocationCoordinate2D
在你的 MapViewController.h 中
@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];
现在您已经有了起点和终点数组,您可以开始绘制它们之间的路线(从 A 到 B,从 B 到 C,从 C 到 D)。
添加一个按钮,然后连接一个 IBAction
,这样您就可以在该方法中施展魔法。
- (IBAction)btnDirectionsPressed:(id)sender {
[self enableUI:NO]; // This method enables or disables all the UI elements that interact with the user
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); // Create the semaphore
__block NSMutableArray *routes = [[NSMutableArray alloc] init]; // Arrays to store MKRoute objects and MKAnnotationPoint objects, so then we can draw them on the map
__block NSMutableArray *annotations = [[NSMutableArray alloc] init];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
for (RouteObject *routeObject in _locations) {
MKDirectionsRequest *directionsRequest = [MKDirectionsRequest new];
[directionsRequest setTransportType:MKDirectionsTransportTypeAutomobile];
[directionsRequest setSource:[routeObject originMapItem]];
[directionsRequest setDestination:[routeObject destinationMapItem]];
[directionsRequest setRequestsAlternateRoutes:NO];
MKDirections *direction = [[MKDirections alloc] initWithRequest:directionsRequest];
// For each object in the locations array, we request that route from its origin and its destination
[direction calculateDirectionsWithCompletionHandler: ^(MKDirectionsResponse *response, NSError *error) {
if (error) {
NSLog(@"There was an error getting your directions");
return;
}
MKRoute *route = [response.routes firstObject];
[routes addObject:route];
[annotations addObject:[routeObject destinationAnnotation]];
dispatch_semaphore_signal(semaphore); // Send the signal that one semaphore is ready to consume
}];
}
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
for (int i = 0; i < _locations.count; i++) {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); // Wait until one semaphore is ready to consume
dispatch_async(dispatch_get_main_queue(), ^{ // For each element, dispatch to the main queue to draw route and annotation corresponding to that location
MKRoute *route = routes[i];
[self.mapView addOverlay:route.polyline];
[self.mapView addAnnotation:annotations[i]];
});
}
dispatch_async(dispatch_get_main_queue(), ^{ // Finally, dispatch to the main queue enabling elements and resizing map to show all the annotations
[self enableUI:YES];
[self fitRegionToRoutes];
});
});
}
希望对您有所帮助!
乔尔。
关于iOS MapKit 通过道路连接 map 上的多个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23529970/