ios - 在 iOS7 的 MKMapView 上显示 MKRoute

标签 ios ios7 mkmapview

我正在使用 MKDirectionsRequest 查找两点之间的 MKRoute。 响应将使用以下代码显示在 MKMapView 上:

MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error)
     {
         if (error)
         {
             NSLog(@"Error : %@", error);
         }
         else
         {
             [_mapView removeOverlays:_mapView.overlays];
             for (MKRoute *route in response.routes)
             {
                 [_mapView insertOverlay:route.polyline atIndex:0 level:MKOverlayLevelAboveRoads];
             }
         }
     }];

我的问题是,如果用户正在移动,是否有适当的方法来更新 MKRoute。我正在使用的自动取款机

- (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在苹果开发文档中。 我想我必须做类似的事情

for (MKRoute *route in response.routes)
{
   for (MKRouteStep *step in route.steps) 
   {
      [_mapView insertOverlay:step.polyline atIndex:0 level:MKOverlayLevelAboveRoads];
   }
}

但我如何识别哪些步骤实际用户位置之后?

编辑:另一种选择可能是使用触发事件的计时器(删除旧覆盖并添加新覆盖)。你怎么看?

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

最佳答案

下面的链接包含在 MKMapView 上显示方向的示例代码,我们只需要在其中传递纬度和经度值。

https://github.com/kadirpekel/MapWithRoutes

关于ios - 在 iOS7 的 MKMapView 上显示 MKRoute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18975677/

相关文章:

ios - 如何通过 Storyboard 中的组件自行调整容器 View 大小

iphone - 在 iPhone 中加载应用程序时出现问题

ios - 在 GMSMapView 中更改用户位置

iOS 7 后台获取 : Large Import Operation

ios - 将一组位置从 tableview 传递到 mapview

iOS - 调用 dispatch_async 时没有堆栈跟踪

ios - 嵌入自定义容器 View Controller 时,内容位于导航栏下方。

iOS 7 UI 转换问题 : Touch events ignored at bottom of screen

ios - 在 MKMapView 中隐藏指南针和法律导致 App Store 拒绝

ios - 不能选择 MKViewAnnotation 两次?