在iOS7中的MKMapView上显示MKRoute

前端之家收集整理的这篇文章主要介绍了在iOS7中的MKMapView上显示MKRoute前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用MKDirectionsRequest在两点之间找到一个MKRoute.
响应将使用以下代码显示在MKMapView上:
  1. MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
  2. [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response,NSError *error)
  3. {
  4. if (error)
  5. {
  6. NSLog(@"Error : %@",error);
  7. }
  8. else
  9. {
  10. [_mapView removeOverlays:_mapView.overlays];
  11. for (MKRoute *route in response.routes)
  12. {
  13. [_mapView insertOverlay:route.polyline atIndex:0 level:MKOverlayLevelAboveRoads];
  14. }
  15. }
  16. }];

我的问题是,如果用户正在移动,如果有正确的方法更新MKRoute.我正在使用Atm

  1. - (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)aUserLocation

识别移动,在这种方法删除MKRoute覆盖,并计算和添加新的用户位置新.
我想知道是否有一种方法计算路线只有一次,并以编程方式更新覆盖?

编辑:
我发现这个https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKRouteStep_class/Reference/Reference.html#//apple_ref/occ/cl/MKRouteStep在苹果开发文档.
我想我必须做一些类似的事情

  1. for (MKRoute *route in response.routes)
  2. {
  3. for (MKRouteStep *step in route.steps)
  4. {
  5. [_mapView insertOverlay:step.polyline atIndex:0 level:MKOverlayLevelAboveRoads];
  6. }
  7. }

但是如何识别实际用户位置背后的哪些步骤?

编辑:另一个选项可能是使用触发事件的定时器(删除旧的叠加层并添加新的叠加层).你怎么看?

  1. [NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(addRouteOverlay) userInfo:nil repeats:YES];

解决方法

以下链接包含用于显示MKMapView路线的示例代码,我们只需要在其中传递纬度和经度值.

https://github.com/kadirpekel/MapWithRoutes

猜你在找的iOS相关文章