简体   繁体   English

在iOS7中的MKMapView上显示MKRoute

[英]Displaying MKRoute on MKMapView in iOS7

I'm using MKDirectionsRequest to find a MKRoute between two points. 我正在使用MKDirectionsRequest来找到两点之间的MKRoute。 The response will be displayed on the MKMapView using this code: 响应将使用以下代码显示在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];
             }
         }
     }];

My question is, if there is a proper way to update the MKRoute if the user is moving. 我的问题是,如果用户正在移动,是否有正确的方法来更新MKRoute。 Atm I'm using 我正在使用Atm

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

to recognize movement and in this method I'm removing the MKRoute overlay and calculating and adding the new with the new user location. 识别移动,在这种方法中,我正在移除MKRoute叠加层,并计算新的用户位置并添加新的用户位置。 I'm wondering if there is a way to compute the route only one time and updating the overlay programmatically? 我想知道是否有一种方法只计算一次路由并以编程方式更新叠加层?

Edit: I found this https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKRouteStep_class/Reference/Reference.html#//apple_ref/occ/cl/MKRouteStep in Apples Dev Docs. 编辑:我在Apples Dev Docs中找到了这个https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKRouteStep_class/Reference/Reference.html#//apple_ref/occ/cl/MKRouteStep I guess i have to do something like 我想我必须做点什么

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

but how can i recognize which steps are behind the actual user location? 但是我如何识别实际用户位置背后的步骤?

Edit: An other option could be to use a timer which triggers the event (removing the old overlay and adding the new one). 编辑:另一个选项可能是使用触发事件的计时器(删除旧的叠加层并添加新的叠加层)。 What do you think? 你怎么看?

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

Below link contains the sample code to show directions on a MKMapView, we only need to pass latitude and longitude values in it. 下面的链接包含在MKMapView上显示方向的示例代码,我们只需要在其中传递纬度和经度值。

https://github.com/kadirpekel/MapWithRoutes https://github.com/kadirpekel/MapWithRoutes

I have a nice solution to do this kind of work. 我有一个很好的解决方案来做这种工作。

Simply, get destination location like below with lat (like -34.059811) and long (like 150.769238). 简单地说,使用lat(如-34.059811)和long(如150.769238)获取下面的目标位置。

CLLocation  *destinationLocation=[[CLLocation alloc] initWithLatitude:-34.059811 longitude:150.769238];

and pass this lat long in below url string,you can directly pass this lat long in string, 并在下面的url字符串中传递这个lat long,你可以在字符串中直接传递这个lat,

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

Now make a url, 现在做一个网址,

NSURL *url = [NSURL URLWithString: [strLink stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];

Open this url in browser or UIWebView and refresh that page with timer and see the magic,route will automatically update according to current location and destination location,you don't need to update current location every time. 在浏览器或UIWebView中打开此URL并使用计时器刷新该页面并查看魔术,路线将根据当前位置和目的地位置自动更新,您不需要每次都更新当前位置。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM