Skip to content

Commit c8aaa8b

Browse files
committed
Encode and decode routable points
1 parent d9e787e commit c8aaa8b

File tree

2 files changed

+52
-22
lines changed

2 files changed

+52
-22
lines changed

MapboxGeocoder/MBGeocoder.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ extension CLLocationCoordinate2D {
7070
/**
7171
Initializes a coordinate pair based on the given GeoJSON array.
7272
*/
73-
internal init(geoJSON array: [Double]) {
73+
internal init(geoJSON array: [CLLocationDegrees]) {
7474
assert(array.count == 2)
7575
self.init(latitude: array[1], longitude: array[0])
7676
}
@@ -83,6 +83,13 @@ extension CLLocation {
8383
internal convenience init(coordinate: CLLocationCoordinate2D) {
8484
self.init(latitude: coordinate.latitude, longitude: coordinate.longitude)
8585
}
86+
87+
/**
88+
Returns a GeoJSON compatible array of coordinates.
89+
*/
90+
internal func geojson() -> [CLLocationDegrees] {
91+
return [coordinate.longitude, coordinate.latitude]
92+
}
8693
}
8794

8895
/**

MapboxGeocoder/MBPlacemark.swift

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ open class Placemark: NSObject, Codable {
7676
case wikidataItemIdentifier = "wikidata"
7777
case properties
7878
case boundingBox = "bbox"
79-
case routableLocations = "routable_points"
80-
}
81-
82-
private enum RoutableLocationsKeys: String, CodingKey {
83-
case points = "points"
8479
}
8580

8681

@@ -114,12 +109,6 @@ open class Placemark: NSObject, Codable {
114109
let northEast = CLLocationCoordinate2D(geoJSON: Array(boundingBox.suffix(2)))
115110
region = RectangularRegion(southWest: southWest, northEast: northEast)
116111
}
117-
118-
if let points = try? container.nestedContainer(keyedBy: RoutableLocationsKeys.self, forKey: .routableLocations),
119-
let coordinatePairs = try points.decodeIfPresent([[CLLocationDegrees]].self, forKey: .points) {
120-
let locations = coordinatePairs.map { CLLocation(coordinate: CLLocationCoordinate2D(geoJSON: $0)) }
121-
routableLocations = locations
122-
}
123112
}
124113

125114
public func encode(to encoder: Encoder) throws {
@@ -364,15 +353,6 @@ open class Placemark: NSObject, Codable {
364353
}
365354
return String(describing: houseNumber)
366355
}
367-
368-
/**
369-
An array of locations that serve as hints for navigating to the placemark.
370-
371-
If the `GeocodeOptions.includesRoutableLocations` property is set to `true`, this property contains locations that are suitable to use as a waypoint in a routing engine such as MapboxDirections.swift. Otherwise, if the `GeocodeOptions.includesRoutableLocations` property is set to `false`, this property is set to `nil`.
372-
373-
For the placemark’s geographic center, use the `location` property. The routable locations may differ from the geographic center. For example, if a house’s driveway leads to a street other than the nearest street (by straight-line distance), then this property may contain the location where the driveway meets the street. A route to the placemark’s geographic center may be impassable, but a route to the routable location would end on the correct street with access to the house.
374-
*/
375-
@objc open var routableLocations: [CLLocation]?
376356
}
377357

378358
internal struct GeocodeResult: Codable {
@@ -412,6 +392,47 @@ internal struct Properties: Codable {
412392
@objc(MBGeocodedPlacemark)
413393
open class GeocodedPlacemark: Placemark {
414394

395+
private enum CodingKeys: String, CodingKey {
396+
case routableLocations = "routable_points"
397+
}
398+
399+
private enum RoutableLocationsKeys: String, CodingKey {
400+
case points = "points"
401+
}
402+
403+
/**
404+
An array of locations that serve as hints for navigating to the placemark.
405+
406+
If the `GeocodeOptions.includesRoutableLocations` property is set to `true`, this property contains locations that are suitable to use as a waypoint in a routing engine such as MapboxDirections.swift. Otherwise, if the `GeocodeOptions.includesRoutableLocations` property is set to `false`, this property is set to `nil`.
407+
408+
For the placemark’s geographic center, use the `location` property. The routable locations may differ from the geographic center. For example, if a house’s driveway leads to a street other than the nearest street (by straight-line distance), then this property may contain the location where the driveway meets the street. A route to the placemark’s geographic center may be impassable, but a route to the routable location would end on the correct street with access to the house.
409+
*/
410+
@objc open var routableLocations: [CLLocation]?
411+
412+
public required init(from decoder: Decoder) throws {
413+
try super.init(from: decoder)
414+
415+
let container = try decoder.container(keyedBy: CodingKeys.self)
416+
417+
if let pointsContainer = try? container.nestedContainer(keyedBy: RoutableLocationsKeys.self, forKey: .routableLocations),
418+
let coordinatePairs = try pointsContainer.decodeIfPresent([[CLLocationDegrees]].self, forKey: .points) {
419+
let locations = coordinatePairs.map { CLLocation(coordinate: CLLocationCoordinate2D(geoJSON: $0)) }
420+
routableLocations = locations
421+
}
422+
}
423+
424+
public override func encode(to encoder: Encoder) throws {
425+
try super.encode(to: encoder)
426+
427+
var container = encoder.container(keyedBy: CodingKeys.self)
428+
429+
if let routableLocations = routableLocations {
430+
var pointsContainer = container.nestedContainer(keyedBy: RoutableLocationsKeys.self, forKey: .routableLocations)
431+
let coordinatePairs = routableLocations.map { $0.geojson() }
432+
try pointsContainer.encodeIfPresent(coordinatePairs, forKey: .points)
433+
}
434+
}
435+
415436
@objc open override var debugDescription: String {
416437
return qualifiedName!
417438
}
@@ -533,4 +554,6 @@ open class GeocodedPlacemark: Placemark {
533554
A concrete subclass of `Placemark` to represent entries in a `GeocodedPlacemark` object’s `superiorPlacemarks` property. These entries are like top-level geocoding results, except that they lack location information and are flatter, with properties directly at the top level.
534555
*/
535556
@objc(MBQualifyingPlacemark)
536-
open class QualifyingPlacemark: Placemark {}
557+
open class QualifyingPlacemark: Placemark {
558+
559+
}

0 commit comments

Comments
 (0)