【问题标题】:latitude and longitude points from MKPolylineMKPolyline 中的经纬度点
【发布时间】:2014-03-18 15:46:03
【问题描述】:

我正在尝试找出一种方法,从在 iOS 应用程序的 MKMapView 上绘制的 MKPolyline 获取所有纬度和经度点。

我知道 MKPolyline 不存储纬度和经度点,但我正在寻找一种方法来构建 MKPolyline 将在地图上触及的经纬度数组。

有人对此有具体的可能解决方案吗?

谢谢

编辑: 看到第一个响应(谢谢)后,我想我需要更好地解释我的代码在做什么:

  1. 首先我在 MKDirections 对象上调用“calculateDirectionsWithCompletionHandler
  2. 我返回具有“polyline”属性的 MKRoute 对象。
  3. 然后我在地图视图上调用“addOverlay从 MKRoute 对象传入折线

就是这样。

所以,我已经为我构建了一条折线。所以我想以某种方式获取折线中找到的所有点并将它们映射到经纬度...

【问题讨论】:

    标签: ios mkmapview mkpolyline


    【解决方案1】:

    要从MKRoute 获取折线的坐标,请使用getCoordinates:range: 方法。
    该方法在 MKMultiPoint 类中,MKPolyline 继承自该类。

    这也意味着这适用于任何折线——无论它是由您创建的还是由MKDirections 创建的。

    您分配一个足够大的 C 数组来保存您想要的坐标数量并指定范围(例如,从第 0 个开始的所有点)。

    例子:

    //route is the MKRoute in this example
    //but the polyline can be any MKPolyline
    
    NSUInteger pointCount = route.polyline.pointCount;
    
    //allocate a C array to hold this many points/coordinates...
    CLLocationCoordinate2D *routeCoordinates 
        = malloc(pointCount * sizeof(CLLocationCoordinate2D));
    
    //get the coordinates (all of them)...
    [route.polyline getCoordinates:routeCoordinates 
                             range:NSMakeRange(0, pointCount)];
    
    //this part just shows how to use the results...
    NSLog(@"route pointCount = %d", pointCount);
    for (int c=0; c < pointCount; c++)
    {
        NSLog(@"routeCoordinates[%d] = %f, %f", 
            c, routeCoordinates[c].latitude, routeCoordinates[c].longitude);
    }
    
    //free the memory used by the C array when done with it...
    free(routeCoordinates);
    

    根据路线,为数百或数千个坐标做好准备。

    【讨论】:

      【解决方案2】:

      Swift 3 版本:

      我知道这是一个非常古老的问题,但在搜索此问题时它仍然是 Google 上的热门话题之一,没有好的 Swift 解决方案,所以想分享我的小扩展,让生活更轻松将coordinates 属性添加到 MKPolyline:

      https://gist.github.com/freak4pc/98c813d8adb8feb8aee3a11d2da1373f

      【讨论】:

      • 谢谢!也适用于 Swift 4。
      猜你喜欢
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多