MapKit中沿路径的注释

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

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

我在MapKit文档中找不到任何forms的支持。 有任何想法吗?

在此处输入图像描述

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

 - (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 )overlay { MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay]; renderer.strokeColor = [UIColor blueColor]; renderer.alpha = 0.7; renderer.lineWidth = 4.0; return renderer; } 

发问者编辑:

最后在这个答案的帮助下使它工作。 我把它添加到下面的代码中,它说这里有魔力

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

也许不是最有效的方式,但应该仍然很快,可能是计算你的起点和终点的中点(即平均他们的拉特和长点)。 然后,遍历您的折线点,检查每个点到该中点的距离。 选择最接近的比赛。

即使线条非常弯曲,中点也会直接在两端之间。 野生曲线上的某些点最接近外部中点,可能是放置注释的好地方。

获得MKRoute ,可以使用以下方法找到近似中心点:

 route.polyline.coordinate 

这将返回一个CLLocationCoordinate2D ,您可以使用它来居中注释。

感谢这是一个老问题,但我一直在寻找类似的东西,并最终手动计算中心。 事实certificate,自iOS 8以来它非常简单。

如果你想知道swift的中间部分,你可以使用以下代码:

MKCoordinateForMapPoint(route.polyline.points()[route.polyline.pointCount/2])

使用范例:

 directions.calculateDirectionsWithCompletionHandler ({ (response: MKDirectionsResponse?, error: NSError?) in if error == nil { self.showRoute(response!) } else{ print("some error") } }) func showRoute(response:MKDirectionsResponse){ for route in response.routes { self.map.addOverlay(route.polyline, level: MKOverlayLevel.AboveRoads) self.map.setCenterCoordinate(route.polyline.coordinate, animated: true) self.map.setRegion(MKCoordinateRegionMakeWithDistance(route.polyline.coordinate, route.distance*0.75, route.distance*0.75), animated: true) let routeAnnotation = MKPointAnnotation() routeAnnotation.coordinate = MKCoordinateForMapPoint(route.polyline.points()[route.polyline.pointCount/2]) map.addAnnotation(routeAnnotation) } } 

我建议你看看这个 ,与路由集成。

Interesting Posts