【问题标题】:Displaying MKRoute on MKMapView in iOS7在 iOS7 中的 MKMapView 上显示 MKRoute
【发布时间】:2013-09-29 07:39:21
【问题描述】:

我正在使用 MKDirectionsRequest 在两点之间查找 MKRoute。 响应将使用以下代码显示在 MKMapView 上:

MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error)
     {
         if (error)
         {
             NSLog(@"Error : %@", error);
         }
         else
         {
             [_mapView removeOverlays:_mapView.overlays];
             for (MKRoute *route in response.routes)
             {
                 [_mapView insertOverlay:route.polyline atIndex:0 level:MKOverlayLevelAboveRoads];
             }
         }
     }];

我的问题是,如果用户正在移动,是否有适当的方法来更新 MKRoute。我正在使用的自动取款机

- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)aUserLocation

为了识别运动,在这种方法中,我删除了 MKRoute 覆盖并计算并添加新的用户位置。 我想知道是否有一种方法可以只计算一次路线并以编程方式更新覆盖?

编辑: 我在 Apples Dev Docs 中找到了这个 https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKRouteStep_class/Reference/Reference.html#//apple_ref/occ/cl/MKRouteStep。 我想我必须做类似的事情

for (MKRoute *route in response.routes)
{
   for (MKRouteStep *step in route.steps) 
   {
      [_mapView insertOverlay:step.polyline atIndex:0 level:MKOverlayLevelAboveRoads];
   }
}

但我如何识别实际用户位置之后的步骤?

编辑: 另一种选择可能是使用触发事件的计时器(删除旧叠加层并添加新叠加层)。你怎么看?

[NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(addRouteOverlay) userInfo:nil repeats:YES];

【问题讨论】:

  • 我不推荐使用计时器,因为即使用户没有移动,您也可能会不必要地更新路线。最简单的选项是在 didUpdateUserLocation 中执行此操作,但前提是用户在绘制最后一条路线后移动了 X 米。不相关,但如果您将纯“iOS”标签添加到问题中,它可能会得到更多关注(以及正确突出代码的语法)。
  • 我以前做过类似的事情,但不知道为什么你只想在用户移动时制作路线图。您可以首次在地图上使用 MKPolyLine 在起点和终点之间制作路线。稍后如果用户移动,您可以使用默认的当前位置符号(蓝色小圆圈)在地图上显示用户的更新位置。请澄清一下,以便我可以提出一些建议,
  • 用户位置显示正确。例如对于从 A->B->C 的路线,用户位置在 A 并且路线显示正确,但是当用户移动到 B 时,路线仍然从 A->C 显示,而不是从 B->C 显示。
  • 我知道这是一个老问题,但我不会使用计时器,而是使用MKMapViewDelegate 方法didUpdateUserLocation 并且仅在上次保存的用户位置的更改显着时更新。 (或者您可以使用CLLocationManager 并通过它获取用户位置更改的通知。)

标签: ios ios7 mkmapview


【解决方案1】:

以下链接包含在 MKMapView 上显示方向的示例代码,我们只需要在其中传递纬度和经度值。

https://github.com/kadirpekel/MapWithRoutes

【讨论】:

  • 像我正在寻找的魅力一样工作
  • 这不能回答问题。该链接指向一个使用 Google api 获取路线的 2 年历史项目。问题是关于如何在 iOS 7 中有效地重用来自 MKDirections 的结果。OP 已经知道如何首先获取方向。
  • @Anna 这是另一个选项,在我们解决这个问题之前可以使用它..:)
【解决方案2】:

我有一个很好的解决方案来做这种工作。

简单地说,使用 lat(如 -34.059811)和 long(如 150.769238)获取如下所示的目标位置。

CLLocation  *destinationLocation=[[CLLocation alloc] initWithLatitude:-34.059811 longitude:150.769238];

并在下面的url字符串中传递这个lat long,你可以直接在字符串中传递这个lat long,

NSString* strLink = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=Current+Location&daddr=%f,%f",destinationLocation.latitude,destinationLocation.longitude];

现在做一个网址,

NSURL *url = [NSURL URLWithString: [strLink stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];

在浏览器或UIWebView中打开这个url,用计时器刷新那个页面,看看神奇的,路由会根据当前位置和目的地位置自动更新,你不需要每次更新当前位置时间。

谢谢。

【讨论】:

    猜你喜欢
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多