【问题标题】:Detect what route the user tap on map检测用户在地图上点击的路线
【发布时间】:2015-11-01 19:33:14
【问题描述】:

我有一个项目,我在地图上显示当前位置和另一个位置之间的方向 (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

【问题讨论】:

标签: ios objective-c xcode mapkit


【解决方案1】:

我想通了。不是那么优雅,但它可以完成工作。

  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 = [继续目的地视图控制器]; 长行 = [发件人标签];

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多