我想在我的 mapView 中显示一个点对点的路线,我使用这个代码来创建路线:
- (IBAction)backToYourCar {
MKPlacemark *sourcePlacemark = [[MKPlacemark alloc] initWithCoordinate:self.annotationForCar.coordinate addressDictionary:nil];
NSLog(@"coordiante : locationIniziale %f", sourcePlacemark.coordinate.latitude);
MKMapItem *carPosition = [[MKMapItem alloc] initWithPlacemark:sourcePlacemark];
MKMapItem *actualPosition = [MKMapItem mapItemForCurrentLocation];
NSLog(@"coordiante : source %f, ActualPosition %f", carPosition.placemark.coordinate.latitude ,actualPosition.placemark.coordinate.latitude);
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
request.source = actualPosition;
request.destination = carPosition;
request.requestsAlternateRoutes = YES;
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
if (error) {
NSLog(@"Error : %@", error);
}
else {
[self showDirections:response]; //response is provided by the CompletionHandler
}
}];
}
这用于在地图上显示路线:
- (void)showDirections:(MKDirectionsResponse *)response
{
for (MKRoute *route in response.routes) {
[self.mapView addOverlay:route.polyline level:MKOverlayLevelAboveRoads];
}
}
实际上这段代码什么都不做。
如果我尝试打印路线的距离,我会得到正确的值:
route distance: 1910.000000
那么路线是对的,但我不明白为什么它没有出现在地图上!
有什么建议么?