简体   繁体   English

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

[英]Detect what route the user tap on map

I have a project where I show directions between current location and another location on a map (MapKit) 我有一个项目,该项目在地图上显示当前位置和另一个位置之间的方向(MapKit)

All works well. 一切正常。 And I can get alternative routes. 我可以获得替代路线。

request.requestsAlternateRoutes = YES;

But when the user tap on a route I show an annotation with distance and some other info. 但是,当用户点击路线时,我会显示带有距离和其他一些信息的注释。 I want to pass this spesific route to another view. 我想将这种特殊的途径传递给另一个观点。 How can I achieve that? 我该如何实现? Like the original Map app on iOS. 就像iOS上的原始Map应用一样。 I can get different routes, and tap on a route to get direction details. 我可以获得不同的路线,然后点击一条路线以获取路线详细信息。

I have googled a lot, and the closest example is this: 我用谷歌搜索了很多,最接近的例子是这样的:

[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];

But _currentRoute is the first one. 但是_currentRoute是第一个。 I want to let the user select currentRoute on tap on the map. 我想让用户在地图上点击选择currentRoute

I figured it out. 我想到了。 Not so elegant, but it does the job. 虽然没有那么优雅,但确实可以做到。

  1. Make an ivar - NSMutableArray *routeArray in the header file. 在头文件中NSMutableArray *routeArray一个ivar- NSMutableArray *routeArray

I also have a MKMapView outlet in my header. 标头中也有MKMapView插座。

  1. In my getDirections method, I tagged the MKMapView instance with a counter and added the route object to the routeArray. 在我的getDirections方法中,我用计数器标记了MKMapView实例,并将路由对象添加到routeArray中。

When the delegate for making the Annotation is run, I can tag the anntotationView with the mapView.tag. 运行用于制作批注的委托时,我可以使用mapView.tag标记anntotationView。

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

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

I pass the view.tag to my segue. 我将view.tag传递给我的segue。

    NSLog(@"Counter tag: %ld", (long)view.tag);
[self performSegueWithIdentifier:@"detaljer" sender:view];
  1. I can then pass my selected route to my new view. 然后,我可以将所选的路线传递到新视图。

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { WeatherTableViewController *routeController = [segue destinationViewController]; -(void)prepareForSegue:(UIStoryboardSegue *)segue发送者:(id)sender {WeatherTableViewController * routeController = [segue destinationViewController]; long row = [sender tag]; 长行= [发件人标签];

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM