检测用户在地图上点击的路线

Detect what route the user tap on map

我有一个项目,我在地图上显示当前位置和另一个位置之间的方向 (MapKit)

一切正常。我可以获得替代路线。

request.requestsAlternateRoutes = YES;

但是当用户点击路线时,我会显示带有距离和其他一些信息的注释。我想将这条特定路线传递给另一个视图。我怎样才能做到这一点?喜欢 iOS 上的原始地图应用程序。我可以获得不同的路线,然后点击路线以获取方向详细信息。

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

[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是第一个。我想让用户 select currentRoute 在地图上点击。

我明白了。不是那么优雅,但它完成了工作。

  1. 在头文件中制作一个ivar - NSMutableArray *routeArray

我的页眉中也有一个 MKMapView 插座。

  1. 在我的 getDirections 方法中,我用计数器标记了 MKMapView 实例并将路由对象添加到 routeArray。

当制作Annotation的委托是运行时,我可以用mapView.tag标记anntotationView。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
  1. 当我点击注释时:

    • (void)mapView:(MKMapView *)mapView annotationView:(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 sender:(id)sender { WeatherTableViewController *routeController = [继续 destinationViewController]; 长行 = [发件人标签];

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