我正在使用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];
我不推薦計時器,因爲即使用戶沒有移動,您也可能會不必要地更新路由。最簡單的選擇是在didUpdateUserLocation中完成,但只有當用戶在繪製完最後一條路線後移動了X米。不相關,但如果您將簡單的「iOS」標記添加到問題中,它可能會引起更多關注(以及語法高亮顯示代碼)。 – Anna
我以前做過這樣的事情,但不知道爲什麼你只想在用戶移動時製作路線圖。您可以在地圖上首次使用MKPolyLine在開始和結束點之間製作路線。稍後,如果用戶移動,則可以使用默認的當前位置符號(藍色小圓圈)在地圖上顯示用戶的更新位置。請澄清以便我可以提出建議, –
用戶位置顯示正確。例如對於從A-> B-> C的路由,用戶位置在A並且路由顯示正確,但是當用戶移動到B時,仍然從A-> C而不是從B-> C顯示路由。 – matthias