2013-09-24 27 views
13

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

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有道。 ATM我使用

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

认识到运动和在这个方法中,我去掉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]; 
    } 
} 

,但我怎样才能识别哪些步骤背后实际用户的位置?

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

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

我不推荐计时器,因为即使用户没有移动,您也可能会不必要地更新路由。最简单的选择是在didUpdateUserLocation中完成,但只有当用户在绘制完最后一条路线后移动了X米。不相关,但如果您将简单的“iOS”标记添加到问题中,它可能会引起更多关注(以及语法高亮显示代码)。 – Anna

+0

我以前做过这样的事情,但不知道为什么你只想在用户移动时制作路线图。您可以在地图上首次使用MKPolyLine在开始和结束点之间制作路线。稍后,如果用户移动,则可以使用默认的当前位置符号(蓝色小圆圈)在地图上显示用户的更新位置。请澄清以便我可以提出建议, –

+0

用户位置显示正确。例如对于从A-> B-> C的路由,用户位置在A并且路由显示正确,但是当用户移动到B时,仍然从A-> C而不是从B-> C显示路由。 – matthias

回答

0

下面的链接包含示例代码来显示MKMapView的方向,我们只需要传递经纬度值就可以了。

https://github.com/kadirpekel/MapWithRoutes

+0

像我正在寻找这个魅力的作品 –

+3

这并不回答这个问题。该链接指向使用Google API获取路线的2年旧项目。问题在于如何有效地重用iOS 7中MKDirections的结果.OP已经知道如何获得方向。 – Anna

+0

@Anna这是一个可以使用,直到我们得到解决这个问题的其他选项.. :) – Ashutosh

-2

我有一个很好的解决方案做这方面的工作。

简单地说,得到像下面一样的目标位置lat(如-34.059811)和long(如150.769238)。

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

,并通过这个纬度长在下面的网址字符串,可以直接通过这个纬度长字符串,

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

现在做一个网址,

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

打开的网址,浏览器或UIWebView和刷新该页面的定时器和看到的魔术,路线会根据当前位置和目的地位置自动更新,你不'每次都需要更新当前位置。

谢谢。