在iOS7中的MKMapView上显示MKRoute

mat*_*ias 13 mkmapview ios ios7

我正在使用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];
             }
         }
     }];
Run Code Online (Sandbox Code Playgroud)

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

- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)aUserLocation
Run Code Online (Sandbox Code Playgroud)

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

编辑: 我在Apples Dev Docs中找到了这个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];
   }
}
Run Code Online (Sandbox Code Playgroud)

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

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

[NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(addRouteOverlay) userInfo:nil repeats:YES];
Run Code Online (Sandbox Code Playgroud)

Pan*_*hit -2

我有一个很好的解决方案来完成此类工作。

简单地说,通过纬度(例如-34.059811)和经度(例如150.769238)获取如下所示的目的地位置。

CLLocation  *destinationLocation=[[CLLocation alloc] initWithLatitude:-34.059811 longitude:150.769238];
Run Code Online (Sandbox Code Playgroud)

并在下面的url字符串中传递这个lat long,您可以直接在字符串中传递这个lat long,

NSString* strLink = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=Current+Location&daddr=%f,%f",destinationLocation.latitude,destinationLocation.longitude];
Run Code Online (Sandbox Code Playgroud)

现在创建一个网址,

NSURL *url = [NSURL URLWithString: [strLink stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
Run Code Online (Sandbox Code Playgroud)

在浏览器或UIWebView中打开此 url并使用计时器刷新该页面,您会看到神奇的效果,路线会根据当前位置和目的地位置自动更新,您无需每次都更新当前位置。

谢谢。