【发布时间】:2013-09-29 07:39:21
【问题描述】:
我正在使用 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 覆盖并计算并添加新的用户位置。 我想知道是否有一种方法可以只计算一次路线并以编程方式更新覆盖?
编辑: 我在 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];
【问题讨论】:
-
我不推荐使用计时器,因为即使用户没有移动,您也可能会不必要地更新路线。最简单的选项是在 didUpdateUserLocation 中执行此操作,但前提是用户在绘制最后一条路线后移动了 X 米。不相关,但如果您将纯“iOS”标签添加到问题中,它可能会得到更多关注(以及正确突出代码的语法)。
-
我以前做过类似的事情,但不知道为什么你只想在用户移动时制作路线图。您可以首次在地图上使用 MKPolyLine 在起点和终点之间制作路线。稍后如果用户移动,您可以使用默认的当前位置符号(蓝色小圆圈)在地图上显示用户的更新位置。请澄清一下,以便我可以提出一些建议,
-
用户位置显示正确。例如对于从 A->B->C 的路线,用户位置在 A 并且路线显示正确,但是当用户移动到 B 时,路线仍然从 A->C 显示,而不是从 B->C 显示。
-
我知道这是一个老问题,但我不会使用计时器,而是使用
MKMapViewDelegate方法didUpdateUserLocation并且仅在上次保存的用户位置的更改显着时更新。 (或者您可以使用CLLocationManager并通过它获取用户位置更改的通知。)