ios – 沿MapKit路线的注释

前端之家收集整理的这篇文章主要介绍了ios – 沿MapKit路线的注释前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用MapKit显示位置之间的路线,我正在寻找一种方法添加与Apple Maps应用程序中的路线注释类似的注释,其中注释显示每个路线的旅行时间(如图所示)下面).我已经正确绘制方向,手头的问题是如何计算沿路线的一对坐标.也就是说,在哪里删除注释.

我以为以某种方式使用MKDirection(它包含完整的方向,一步一步),但我不知道如何生成一对位于路线中间的坐标.

我在MapKit文档中找不到任何类型的支持.有任何想法吗?

这是我如何生成路由并显示它.

  1. - (void)generateRoute {
  2. MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
  3. request.source = [MKMapItem mapItemForCurrentLocation];
  4. request.destination = self.destinationMapItem;
  5. MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
  6.  
  7. [directions calculateDirectionsWithCompletionHandler:
  8. ^(MKDirectionsResponse *response,NSError *error) {
  9. if (error) {
  10. // Handle Error
  11. } else {
  12. [self showRoute:response];
  13. }
  14. }];
  15. }
  16.  
  17. - (void)showRoute:(MKDirectionsResponse *)response {
  18. [self.mapView removeOverlays:self.mapView.overlays];
  19. for (MKRoute *route in response.routes)
  20. {
  21. [self.mapView addOverlay:route.polyline level:MKOverlayLevelAboveRoads];
  22. }
  23. [self fitRegionToRoute];
  24. }
  25.  
  26. - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
  27. {
  28. MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
  29. renderer.strokeColor = [UIColor blueColor];
  30. renderer.alpha = 0.7;
  31. renderer.lineWidth = 4.0;
  32.  
  33. return renderer;
  34. }

解决方法

问题编辑:

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

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

原来的答案:

我不知道这是否会有效.只是我的想法在你的问题.

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

  1. MKDirectionsRequest *request =
  2. [[MKDirectionsRequest alloc] init];
  3. request.source = [MKMapItem mapItemForCurrentLocation];
  4. request.destination = _destination;
  5. request.requestsAlternateRoutes = NO;
  6. MKDirections *directions =
  7. [[MKDirections alloc] initWithRequest:request];
  8.  
  9. [directions calculateDirectionsWithCompletionHandler:
  10. ^(MKDirectionsResponse *response,NSError *error) {
  11. if (error) {
  12. // Handle error
  13. } else {
  14. for (MKRoute *route in response.routes)
  15. {
  16. [_routeMap addOverlay:route.polyline level:MKOverlayLevelAboveRoads];
  17. //Here do the magic
  18. //MKPolyline confronts to MKOverlay so you can get the coordinate like
  19. //route.polyline.coordinate once you get the coordinate then you can build
  20. //a annotation. A annotation is nothing but a coordinate with some title.
  21. //According to MKOverlay coordinate property it justs gives you the
  22. //center point of the overlay area
  23. [self createAndAddAnnotationForCoordinate:route.polyline.coordinate]
  24. }
  25. }
  26. }];

添加注释

  1. -(void) createAndAddAnnotationForCoordinate : (CLLocationCoordinate2D) coordinate{
  2. MyAnnotation* annotation= [[MyAnnotation alloc] init];
  3. annotation.coordinate = coordinate;
  4.  
  5. annotation.title = @"Any Title";
  6. annotation.subtitle = @"Any Subtitle";
  7.  
  8. [yourMap addAnnotation: annotation];
  9.  
  10. }

猜你在找的iOS相关文章