Created
June 18, 2019 07:51
-
-
Save yllan/61479c4bd508e17938724dbade087753 to your computer and use it in GitHub Desktop.
How to encode MKPolyline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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