Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode and Decode Placemark.relevance #157

Merged
merged 1 commit into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 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,13 @@ open class GeocodedPlacemark: Placemark {
return properties?.maki
}

/**
A numerical score from 0 (least relevant) to 0.99 (most relevant) measuring
how well each returned feature matches the query. Use this property to
remove results that don’t fully match the query.
*/
@objc 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, "addressPlacemark.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