Skip to content

Commit

Permalink
Encode and Decode Placemark.relevance
Browse files Browse the repository at this point in the history
  • Loading branch information
frederoni committed Aug 27, 2018
1 parent 049d8c9 commit 8cf37ad
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
15 changes: 11 additions & 4 deletions MapboxGeocoder/MBPlacemark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ open class GeocodedPlacemark: Placemark {

private enum CodingKeys: String, CodingKey {
case routableLocations = "routable_points"
case relevance
}

private enum PointsCodingKeys: String, CodingKey {
Expand All @@ -421,8 +422,6 @@ open class GeocodedPlacemark: Placemark {
@objc open var routableLocations: [CLLocation]?

public required init(from decoder: Decoder) throws {
try super.init(from: decoder)

let container = try decoder.container(keyedBy: CodingKeys.self)

if let pointsContainer = try? container.nestedContainer(keyedBy: PointsCodingKeys.self, forKey: .routableLocations),
Expand All @@ -433,13 +432,17 @@ open class GeocodedPlacemark: Placemark {
routableLocations = [CLLocation(coordinate: coordinate)]
}
}

relevance = try container.decodeIfPresent(Double.self, forKey: .relevance) ?? -1

try super.init(from: decoder)
}

public override func encode(to encoder: Encoder) throws {
try super.encode(to: encoder)

var container = encoder.container(keyedBy: CodingKeys.self)

try container.encodeIfPresent(relevance, forKey: .relevance)

if let routableLocations = routableLocations,
!routableLocations.isEmpty {
var pointsContainer = container.nestedContainer(keyedBy: PointsCodingKeys.self, forKey: .routableLocations)
Expand All @@ -448,6 +451,8 @@ open class GeocodedPlacemark: Placemark {
routableLocations[0].coordinate.latitude])
try coordinatesContainer.encode(routableLocation)
}

try super.encode(to: encoder)
}

@objc open override var debugDescription: String {
Expand Down Expand Up @@ -486,6 +491,8 @@ open class GeocodedPlacemark: Placemark {
return properties?.maki
}

open var relevance: Double?

private var clippedAddressLines: [String] {
let lines = qualifiedNameComponents
if scope == .address {
Expand Down
1 change: 0 additions & 1 deletion MapboxGeocoder/MBPlacemarkScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ extension PlacemarkScope: CustomStringConvertible {
scope.update(with: .neighborhood)
case "address":
scope.update(with: .address)

case "poi.landmark":
scope.update(with: .landmark)
case "poi":
Expand Down
2 changes: 1 addition & 1 deletion MapboxGeocoderTests/ForwardGeocodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ForwardGeocodingTests: XCTestCase {

XCTAssertEqual(addressPlacemark.routableLocations![0].coordinate.longitude, CLLocationDegrees(138.995284))
XCTAssertEqual(addressPlacemark.routableLocations![0].coordinate.latitude, CLLocationDegrees(-34.470403))

XCTAssertEqual(addressPlacemark.relevance, 0.39, "Placeshould.relevance should be 0.39")
XCTAssertEqual(addressPlacemark.description, "Pennsylvania Ave", "forward geocode should populate description")
XCTAssertEqual(addressPlacemark.debugDescription, "Pennsylvania Ave, Wasaga Beach, Ontario L9Z 3A8, Canada", "forward geocode should populate debug description")
XCTAssertEqual(addressPlacemark.name, "Pennsylvania Ave", "forward geocode should populate name")
Expand Down

0 comments on commit 8cf37ad

Please sign in to comment.