ios - MapKit 沿线标注

标签 ios iphone ios7 mkmapview mapkit

我正在使用 MapKit 来显示位置之间的方向,我正在寻找一种方法来添加与 Apple Maps 应用程序中的路线注释类似的注释,其中注释显示每条路线的旅行时间(如图所示在下图中)。我已经正确地绘制了方向,手头的问题是如何计算沿线的一对坐标。即,在哪里放置注释。

我想过以某种方式使用 MKDirection(它包含完整的方向,一步一步),但我不确定如何生成一对位于 route 间某处的坐标。

我无法在 MapKit 文档中找到对此的任何类型的支持。有什么想法吗?

enter image description here

这就是我生成路线并显示它的方式。

- (void)generateRoute {
    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
    request.source = [MKMapItem mapItemForCurrentLocation];
    request.destination = self.destinationMapItem;
    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];

    [directions calculateDirectionsWithCompletionHandler:
     ^(MKDirectionsResponse *response, NSError *error) {
         if (error) {
             // Handle Error
         } else {
             [self showRoute:response];
         }
     }];
}

- (void)showRoute:(MKDirectionsResponse *)response {
    [self.mapView removeOverlays:self.mapView.overlays];
    for (MKRoute *route in response.routes)
    {
        [self.mapView addOverlay:route.polyline level:MKOverlayLevelAboveRoads];
    }
    [self fitRegionToRoute];
}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
{
    MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
    renderer.strokeColor = [UIColor blueColor];
    renderer.alpha = 0.7;
    renderer.lineWidth = 4.0;

    return renderer;
}

最佳答案

提问者的编辑:

终于在这个答案的帮助下让它工作了。我将其添加到下面的代码中,它说 Here do the magic:

MKMapPoint middlePoint = route.polyline.points[route.polyline.pointCount/2];
[self createAndAddAnnotationForCoordinate:MKCoordinateForMapPoint(middlePoint)];

原答案:

我不知道这是否有效。只是我对你的问题的看法。

我猜你会创建如下路线 (检查我的内联评论)

MKDirectionsRequest *request = 
       [[MKDirectionsRequest alloc] init];
request.source = [MKMapItem mapItemForCurrentLocation];
request.destination = _destination;
request.requestsAlternateRoutes = NO;
MKDirections *directions = 
       [[MKDirections alloc] initWithRequest:request];

    [directions calculateDirectionsWithCompletionHandler:
 ^(MKDirectionsResponse *response, NSError *error) {
     if (error) {
         // Handle error
     } else {
         for (MKRoute *route in response.routes)
         {
             [_routeMap addOverlay:route.polyline level:MKOverlayLevelAboveRoads];
             //Here do the magic
             //MKPolyline confronts to MKOverlay so you can get the coordinate like 
             //route.polyline.coordinate once you get the coordinate then you can build
             //a annotation. A annotation is nothing but a coordinate with some title.
             //According to MKOverlay coordinate property it justs gives you the 
             //center point of the overlay area
             [self createAndAddAnnotationForCoordinate:route.polyline.coordinate]
         }
     }
 }];

添加注释

-(void) createAndAddAnnotationForCoordinate : (CLLocationCoordinate2D) coordinate{
    MyAnnotation* annotation= [[MyAnnotation alloc] init];
    annotation.coordinate = coordinate;

    annotation.title = @"Any Title";
    annotation.subtitle = @"Any Subtitle";

   [yourMap addAnnotation: annotation];

}

关于ios - MapKit 沿线标注,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20995787/

相关文章:

ios - Swift 3 UITableViewCell indexPath.row 搞砸了

ios - 如何使用 Swift 在 UITableView 中显示系统字体

iphone - 维护不同 View 将使用的网络连接的正确方法是什么?

ios - 当添加到 UISearchDisplayController 时,UISearchBar 在状态栏下剪裁

iOS 7 创建一个像 Photo 和 AppStore 一样的底部 UIToolbar

ios - 如何在 Swift 3 中用户触摸时从 UITextField 中删除文本?

ios - 使用 OpenSsl 支持构建 Kivy-ios

iphone - 在 iPhone 应用程序中 如何更改整个应用程序的字体样式?

iphone - 在 View 之间连接委托(delegate)时出现问题

ios - NSJSONSerialization 弄乱了结果 NSDictionary