簡體   English   中英

檢測用戶在地圖上點擊的路線

[英]Detect what route the user tap on map

我有一個項目,該項目在地圖上顯示當前位置和另一個位置之間的方向(MapKit)

一切正常。 我可以獲得替代路線。

request.requestsAlternateRoutes = YES;

但是,當用戶點擊路線時,我會顯示帶有距離和其他一些信息的注釋。 我想將這種特殊的途徑傳遞給另一個觀點。 我該如何實現? 就像iOS上的原始Map應用一樣。 我可以獲得不同的路線,然后點擊一條路線以獲取路線詳細信息。

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

[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

我想到了。 雖然沒有那么優雅,但確實可以做到。

  1. 在頭文件中NSMutableArray *routeArray一個ivar- NSMutableArray *routeArray

標頭中也有MKMapView插座。

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

運行用於制作批注的委托時,我可以使用mapView.tag標記anntotationView。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
  1. 當我點擊注釋時:

    • (void)mapView:(MKMapView *)mapView注解View:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

我將view.tag傳遞給我的segue。

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

    -(void)prepareForSegue:(UIStoryboardSegue *)segue發送者:(id)sender {WeatherTableViewController * routeController = [segue destinationViewController]; 長行= [發件人標簽];

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM