Build Status CocoaPods Swift 5 Licence

Swift 中的折线编码器/解码器

  1. 特性
  2. 要求
  3. 集成
  4. 用法
  5. 备注
  6. 贡献者
  7. 许可证

特性

计划在未来版本中发布

计划在支持可用时进行

要求


集成

要在您的项目中使用此库,您可以使用 CocoaPods、Carthage、Swift Package Manager 和/或手动集成

CocoaPods

您可以像这样在您的 Podfile 中集成 Polyline

pod 'Polyline', '~> 5.0'

Carthage

您可以像这样在您的 Cartfile 中集成 Polyline

github "raphaelmor/Polyline" ~> 5.0

Swift Package Manager

要使用 Xcode 中的 Swift Package Manager 将 Polyline 集成到应用程序中

  1. 转到 File ‣ Swift Packages ‣ Add Package Dependency。
  2. 输入 https://github.com/raphaelmor/Polyline.git 作为包仓库,然后单击 Next。
  3. 将 Rules 设置为 Version,Up to Next Major,然后输入 5.0.2 作为最低版本要求。单击 Next。

或者,要将 Polyline 集成到另一个 Swift 包中,请将以下包添加到 Package.swift 文件中的 dependencies

.package(url: "https://github.com/raphaelmor/Polyline.git", from: "5.0.2")

手动

要手动安装 Polyline,请将 Polyline.xcodeproj 添加到 Xcode 工作区,然后将您的应用程序链接到 Polyline.framework。

从技术上讲,可以通过将 Polyline.swift 和 CoreLocation.swift 直接添加到您的项目中来安装 Polyline,但不建议这样做,因为它可能会遗漏未来重要的文件。

用法

折线编码

使用 [CLLocationCoordinate2D](推荐)

let coordinates = [CLLocationCoordinate2D(latitude: 40.2349727, longitude: -3.7707443),
CLLocationCoordinate2D(latitude: 44.3377999, longitude: 1.2112933)]

let polyline = Polyline(coordinates: coordinates)
let encodedPolyline: String = polyline.encodedPolyline

// Or for a functional approach :
let encodedPolyline: String = encodeCoordinates(coordinates)

使用 [CLLocation]

let locations = [CLLocation(latitude: 40.2349727, longitude: -3.7707443),
CLLocation(latitude: 44.3377999, longitude: 1.2112933)]

let polyline = Polyline(locations: locations)
let encodedPolyline: String = polyline.encodedPolyline

// Or for a functional approach :
let encodedPolyline: String = encodeLocations(locations)

您也可以编码级别

let levels: [UInt32] = [0,1,2,255]

let polyline = Polyline(coordinates: coordinates, levels: levels)
let encodedLevels: String? = polyline.encodedLevels

// Or for a functional approach :
let encodedLevels: String = encodedLevels(levels)

折线解码

您可以解码为 [CLLocationCoordinate2D](推荐)

let polyline = Polyline(encodedPolyline: "qkqtFbn_Vui`Xu`l]")
let decodedCoordinates: [CLLocationCoordinate2D]? = polyline.coordinates

// Or for a functional approach :
let coordinates: [CLLocationCoordinate2D]? = decodePolyline("qkqtFbn_Vui`Xu`l]")

您也可以解码为 [CLLocation]

let polyline = Polyline(encodedPolyline: "qkqtFbn_Vui`Xu`l]")
let decodedLocations: [CLLocation]? = polyline.locations

// Or for a functional approach :
let locations: [CLLocation]? = decodePolyline("qkqtFbn_Vui`Xu`l]")

您也可以解码级别

let polyline = Polyline(encodedPolyline: "qkqtFbn_Vui`Xu`l]", encodedLevels: "BA")
let decodedLevels: [UInt32]? = polyline.levels

// Or for a functional approach :
let levels: [UInt32]? = decodeLevels("BA")

折线精度

默认精度为 1e5 : 0.12345(谷歌使用的 5 位精度),但您可以在所有 API 方法(和函数)中指定自己的精度。

// OSRM uses a 6 digit precision
let polyline = Polyline(encodedPolyline: "ak{hRak{hR", precision: 1e6)

备注

此库尝试使结果与 Google Maps iOS SDK 生成的折线保持一致。用于编码折线的在线工具有一些关于舍入的细微不一致之处(例如,0.000015 对于纬度四舍五入为 0.00002,但对于经度四舍五入为 0.00001)。

此代码尝试遵守 GitHub Swift 风格指南

贡献者

许可证

Polyline 在 MIT 许可证下发布。有关更多信息,请参阅 LICENSE.txt