Skip to content

Instantly share code, notes, and snippets.

@yllan
Created June 18, 2019 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yllan/61479c4bd508e17938724dbade087753 to your computer and use it in GitHub Desktop.
Save yllan/61479c4bd508e17938724dbade087753 to your computer and use it in GitHub Desktop.
How to encode MKPolyline
import MapKit
extension MKPolyline: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
for idx in 0..<self.pointCount {
let p = self.points()[idx]
try container.encode(p.x)
try container.encode(p.y)
}
}
}
let pts = UnsafeMutablePointer<MKMapPoint>.allocate(capacity: 3)
pts[0] = MKMapPoint(x: 0.5, y: 0.5)
pts[1] = MKMapPoint(x: 0.2, y: 0.2)
pts[2] = MKMapPoint(x: 1.0, y: 1.0)
let polyline = MKPolyline(points: pts, count: 3)
String(data: JSONEncoder().encode(polyline), encoding: .utf8)
// $R3: String? = "[0.5,0.5,0.20000000000000001,0.20000000000000001,1,1]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment