【问题标题】:How do I decode an "encoded polyline" from Mapbox on iOS如何在 iOS 上从 Mapbox 解码“编码折线”
【发布时间】:2021-11-16 10:13:57
【问题描述】:

我有一条由 Mapbox javascript API (https://github.com/mapbox/polyline) 创建的编码折线,我想在 iOS 应用上将此折线解码为坐标列表(甚至只是一个 MGLShape)。

我尝试直接解码为 MGLShape(注意:这是完全错误的,因为我将编码的折线和 GeoJSON 混淆了,而这正是 MGLShape(data:) 正在寻找的内容。)

if let shapeData = "_nwfFteckVd@rIvEhEaB~Ht@lIxD~EvE~DfB|HdCeHrBwHnGT|ExDbGzAjGAyC{GjG[fGbA~@iICuIjG@lFqCrBpHj@lIaClHmGOiEbFVvIkCdHgEdFkGImGQoFbDgGgAmGl@iG_@}F_CmCgHZuI`@wIy@oIk@oI|BkH}EgE}BmH".data(using: .utf8) {
  MGLPolyline
  let shape = try? MGLShape(data: shapeData, encoding: String.Encoding.utf8.rawValue)
  let shapeSource = MGLShapeSource(identifier: "polyLineSource", shape: shape, options: nil)
}

但是shape出来nil

我没有看到任何使用 Mapbox API 轻松解码坐标中的折线的方法。

【问题讨论】:

    标签: ios swift mapbox polyline mapbox-ios


    【解决方案1】:

    事实证明,来自 Mapbox SDK 的编码折线实际上是通过 Google 算法编码的,幸运的是,您可以访问 Interactive Encoder Utility website 并查看编码的折线是否正确。

    一旦我发现它是同一种编码折线(与 GeoJSON 无关),我发现这个 Swift polyline encoder/decoder library 非常适合我的需求。

    // Using the swift polyline library, I can get the coordinates from the polyline and continue making my overlay normally
    let coordinates = Polyline(encodedPolyline: encodedPolyline).coordinates
    

    【讨论】:

      猜你喜欢
      • 2015-12-05
      • 2017-02-12
      • 2012-02-11
      • 1970-01-01
      • 2021-03-23
      • 2014-12-15
      • 1970-01-01
      • 2015-01-18
      • 1970-01-01
      相关资源
      最近更新 更多