在 iOS 7 地图上显示路线:addOverlay 没有效果 [英] display route on iOS 7 maps: addOverlay has no effect
本文介绍了在 iOS 7 地图上显示路线:addOverlay 没有效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的地图视图中显示一个点到点的路线,我使用这个代码来创建路线:
i want display a point to point route inside my mapView, i use this code for create the route:
- (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
}
}];
}
这是为了在地图上显示路线:
and this for show the route on the map:
- (void)showDirections:(MKDirectionsResponse *)response
{
for (MKRoute *route in response.routes) {
[self.mapView addOverlay:route.polyline level:MKOverlayLevelAboveRoads];
}
}
实际上这段代码什么都不做.
actually this code does nothing.
如果我尝试打印路线的距离,我会得到正确的值:
if i try to print the the distance of route i get the correct value:
route distance: 1910.000000
那么路线是对的,但我不明白为什么它没有出现在地图上!
then the route is right, but i can't understand why it doesn't appear on the map!
有什么建议吗?
推荐答案
经过一天的研究,我已经通过这 3 个步骤解决了:
after a day of research i have solved with this 3 steps:
- 设置委托(
self.mapView.delegate = self
). - 导入
MKMapViewDelegate
- 实现新的 iOS7 MapView 委托方法:
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay(id
:)overlay
这是我的实现:
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolyline class]]) {
MKPolyline *route = overlay;
MKPolylineRenderer *routeRenderer = [[MKPolylineRenderer alloc] initWithPolyline:route];
routeRenderer.strokeColor = [UIColor blueColor];
return routeRenderer;
}
else return nil;
}
当您在地图上添加折线时,委托会自动调用此方法.
this method is automatically called by the delegate when you add the polyline on the map.
这篇关于在 iOS 7 地图上显示路线:addOverlay 没有效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文