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

时间:2015-10-30 08:22:06

标签: ios objective-c xcode mapkit

我有一个项目,我在当前位置和地图上的另一个位置(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 个答案:

答案 0 :(得分:0)

我明白了。不是那么优雅,但它确实起作用。

  1. 在标题文件中创建一个ivar - NSMutableArray *routeArray
  2. 我的标题中还有一个MKMapView插座。

    1. 在我的getDirections方法中,我用计数器标记了MKMapView实例,并将路由对象添加到routeArray。
    2. 当运行Annotation的委托时,我可以使用mapView.tag标记anntotationView。

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

        • (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
      2. 我将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 = [segue destinationViewController]; long row = [sender tag];

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