2015-10-30 46 views
2

用戶水龍頭我有一個項目,我顯示在地圖上(MapKit)當前位置和另一個位置之間的路線檢測什麼路線在地圖上

一切運作良好。我可以獲得替代路線。

request.requestsAlternateRoutes = YES; 

但是當用戶點擊一條路線時,我會顯示帶有距離和一些其他信息的註釋。我想通過這個特定的路線到另一個視圖。我怎樣才能做到這一點?就像iOS上的原始地圖應用程序一樣。我可以得到不同的路線,並點擊路線獲取方向細節。

我用Google搜索了很多,最近的例子是這樣的:

[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {   
// Now handle the result 
if (error) { 
    NSLog(@"There was an error getting your directions"); 
    return; 
} 

_currentRoute = [response.routes firstObject]; 

_currentRoute是第一位的。我想讓用戶在地圖上點擊選擇currentRoute

+0

您應該檢測MKOverlay上的接觸。另請參見http://stackoverflow.com/questions/20858108/detecting-touches-on-mkoverlay-in-ios7-mkoverlayrenderer –

+0

但我看不到我的水龍頭如何識別來自MKRoute對象的路線。 –

回答

0

我想通了。不那麼優雅,但是它完成了這項工作。

  1. 在頭文件中做一個伊娃 - NSMutableArray *routeArray

我的頭裏還有一個MKMapView插座。

  1. 在我的getDirections方法中,我用計數器標記了MKMapView實例,並將route對象添加到了routeArray中。

當執行Annotation的委託運行時,我可以使用mapView.tag標記anntotationView。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
  • 當我在註釋輕按:

    • (無效)的MapView:(的MKMapView *)的MapView annotationView:(MKAnnotationView *)視圖calloutAccessoryControlTapped :(UIControl *)控制
  • 我通過view.tag我SEGUE。

    NSLog(@"Counter tag: %ld", (long)view.tag); 
    [self performSegueWithIdentifier:@"detaljer" sender:view]; 
    
  • 我可以然後我所選擇的路線傳遞給我的新視圖。

    - (無效)prepareForSegue:(UIStoryboardSegue *)賽格瑞發件人:(ID)發送方 { WeatherTableViewController * routeController = [原因請看destinationViewController]; long row = [發件人標籤];

    MKRoute * route = routeArray [row]; routeController.selectedRoute = route; }