我正在使用MapKit显示位置之间的路线,我正在寻找一种方法来添加与Apple Maps应用程序中的路线注释类似的注释,其中注释显示每个路线的旅行时间(如图所示)下面).我已经正确绘制方向,手头的问题是如何计算沿路线的一对坐标.也就是说,在哪里删除注释.
我以为以某种方式使用MKDirection(它包含完整的方向,一步一步),但我不知道如何生成一对位于路线中间的坐标.
我在MapKit文档中找不到任何类型的支持.有任何想法吗?
- - (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;
- }
解决方法
问题编辑:
最后在这个答案的帮助下,它的工作.我把它添加到下面的代码中,在这里说这里有魔术:
- 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];
- }