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/19924890/

相关文章:

ios7 - SKPhysicsJointPin lowerAngleLimit 和 upperAngleLimit 值

ios在不退出应用程序的情况下以编程方式进行调用

ios - 带有 sns 主题和 boto3 的静默苹果推送通知

iphone - NSCaseInSensitive在iOS中

iphone - 在 iPad 上呈现长文档

iphone - UIView 以编程方式分配,未在我给出的 x 和 y 处分配。超越这一点。

ios - View Controller 在 iOS 7 中不旋转

iphone - UIAlertView + 模态视图 Controller 不起作用

javascript - 强制由 anchor 链接的视频在 iPad 上全屏播放

ios - 无法在标签或文本字段中显示字符串