Skip to content

Commit 5332289

Browse files
committed
Encode and Decode Placemark.relevance
1 parent 049d8c9 commit 5332289

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

MapboxGeocoder/MBPlacemark.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ open class GeocodedPlacemark: Placemark {
405405

406406
private enum CodingKeys: String, CodingKey {
407407
case routableLocations = "routable_points"
408+
case relevance
408409
}
409410

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

423424
public required init(from decoder: Decoder) throws {
424-
try super.init(from: decoder)
425-
426425
let container = try decoder.container(keyedBy: CodingKeys.self)
427426

428427
if let pointsContainer = try? container.nestedContainer(keyedBy: PointsCodingKeys.self, forKey: .routableLocations),
@@ -433,13 +432,17 @@ open class GeocodedPlacemark: Placemark {
433432
routableLocations = [CLLocation(coordinate: coordinate)]
434433
}
435434
}
435+
436+
relevance = try container.decodeIfPresent(Double.self, forKey: .relevance) ?? -1
437+
438+
try super.init(from: decoder)
436439
}
437440

438441
public override func encode(to encoder: Encoder) throws {
439-
try super.encode(to: encoder)
440-
441442
var container = encoder.container(keyedBy: CodingKeys.self)
442443

444+
try container.encodeIfPresent(relevance, forKey: .relevance)
445+
443446
if let routableLocations = routableLocations,
444447
!routableLocations.isEmpty {
445448
var pointsContainer = container.nestedContainer(keyedBy: PointsCodingKeys.self, forKey: .routableLocations)
@@ -448,6 +451,8 @@ open class GeocodedPlacemark: Placemark {
448451
routableLocations[0].coordinate.latitude])
449452
try coordinatesContainer.encode(routableLocation)
450453
}
454+
455+
try super.encode(to: encoder)
451456
}
452457

453458
@objc open override var debugDescription: String {
@@ -486,6 +491,13 @@ open class GeocodedPlacemark: Placemark {
486491
return properties?.maki
487492
}
488493

494+
/**
495+
A numerical score from 0 (least relevant) to 0.99 (most relevant) measuring
496+
how well each returned feature matches the query. Use this property to
497+
remove results that don’t fully match the query.
498+
*/
499+
@objc open var relevance: Double
500+
489501
private var clippedAddressLines: [String] {
490502
let lines = qualifiedNameComponents
491503
if scope == .address {

MapboxGeocoder/MBPlacemarkScope.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ extension PlacemarkScope: CustomStringConvertible {
2424
scope.update(with: .neighborhood)
2525
case "address":
2626
scope.update(with: .address)
27-
2827
case "poi.landmark":
2928
scope.update(with: .landmark)
3029
case "poi":

MapboxGeocoderTests/ForwardGeocodingTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class ForwardGeocodingTests: XCTestCase {
4141

4242
XCTAssertEqual(addressPlacemark.routableLocations![0].coordinate.longitude, CLLocationDegrees(138.995284))
4343
XCTAssertEqual(addressPlacemark.routableLocations![0].coordinate.latitude, CLLocationDegrees(-34.470403))
44-
44+
XCTAssertEqual(addressPlacemark.relevance, 0.39, "addressPlacemark.relevance should be 0.39")
4545
XCTAssertEqual(addressPlacemark.description, "Pennsylvania Ave", "forward geocode should populate description")
4646
XCTAssertEqual(addressPlacemark.debugDescription, "Pennsylvania Ave, Wasaga Beach, Ontario L9Z 3A8, Canada", "forward geocode should populate debug description")
4747
XCTAssertEqual(addressPlacemark.name, "Pennsylvania Ave", "forward geocode should populate name")

0 commit comments

Comments
 (0)