Skip to content

Commit 910eed2

Browse files
committed
Fix wikidata and address
1 parent fa11f5b commit 910eed2

File tree

1 file changed

+22
-37
lines changed

1 file changed

+22
-37
lines changed

MapboxGeocoder/MBPlacemark.swift

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ open class Placemark: NSObject, Codable {
7373
case superiorPlacemarks = "context"
7474
case center = "center"
7575
case code = "short_code"
76+
case wikidataItemIdentifier = "wikidata"
77+
case properties = "properties"
7678
}
7779

7880
/**
@@ -91,6 +93,12 @@ open class Placemark: NSObject, Codable {
9193
}
9294

9395
code = try container.decodeIfPresent(String.self, forKey: .code)?.uppercased()
96+
if let identifier = try container.decodeIfPresent(String.self, forKey: .wikidataItemIdentifier) {
97+
assert(identifier.hasPrefix("Q"))
98+
wikidataItemIdentifier = identifier
99+
}
100+
101+
properties = try container.decodeIfPresent(Properties.self, forKey: .properties)
94102
}
95103

96104
public func encode(to encoder: Encoder) throws {
@@ -105,6 +113,8 @@ open class Placemark: NSObject, Codable {
105113
}
106114

107115
try container.encode(code, forKey: .code)
116+
try container.encode(wikidataItemIdentifier, forKey: .wikidataItemIdentifier)
117+
try container.encode(properties, forKey: .properties)
108118
}
109119

110120
@objc open override var hashValue: Int {
@@ -131,6 +141,8 @@ open class Placemark: NSObject, Codable {
131141
*/
132142
fileprivate var identifier: String
133143

144+
fileprivate var properties: Properties?
145+
134146
/**
135147
The common name of the placemark.
136148

@@ -172,9 +184,7 @@ open class Placemark: NSObject, Codable {
172184

173185
The Wikidata item contains structured information about the feature represented by the placemark. It also links to corresponding entries in various free content or open data resources, including Wikipedia, Wikimedia Commons, Wikivoyage, and Freebase.
174186
*/
175-
@objc open var wikidataItemIdentifier: String? {
176-
return nil
177-
}
187+
@objc open var wikidataItemIdentifier: String?
178188

179189
/**
180190
An array of keywords that describe the genre of the point of interest represented by the placemark.
@@ -350,8 +360,14 @@ struct GeocodeResult: Codable {
350360
struct Properties: Codable {
351361
private enum CodingKeys: String, CodingKey {
352362
case shortCode = "short_code"
363+
case phoneNumber = "tel"
364+
case maki
365+
case address
353366
}
354367
let shortCode: String?
368+
let maki: String?
369+
let phoneNumber: String?
370+
let address: String?
355371
}
356372

357373
/**
@@ -360,16 +376,6 @@ struct Properties: Codable {
360376
@objc(MBGeocodedPlacemark)
361377
open class GeocodedPlacemark: Placemark {
362378

363-
private enum CodingKeys: String, CodingKey {
364-
case properties
365-
}
366-
367-
public required init(from decoder: Decoder) throws {
368-
let container = try decoder.container(keyedBy: CodingKeys.self)
369-
properties = try container.decodeIfPresent(Properties.self, forKey: .properties)
370-
try super.init(from: decoder)
371-
}
372-
373379
@objc open override var debugDescription: String {
374380
return qualifiedName!
375381
}
@@ -412,18 +418,6 @@ open class GeocodedPlacemark: Placemark {
412418
// }
413419
// }
414420

415-
internal var properties: Properties?
416-
417-
@objc open override var wikidataItemIdentifier: String? {
418-
// TODO: Fix
419-
// let item = propertiesJSON["wikidata"] as? String
420-
// if let item = item {
421-
// assert(item.hasPrefix("Q"))
422-
// }
423-
// return item
424-
return ""
425-
}
426-
427421
@objc open override var genres: [String]? {
428422
// TODO: Fix
429423
// let categoryList = propertiesJSON["category"] as? String
@@ -489,6 +483,8 @@ open class GeocodedPlacemark: Placemark {
489483
var addressDictionary: [String: Any] = [:]
490484
if scope == .address {
491485
addressDictionary[MBPostalAddressStreetKey] = name
486+
} else if let address = properties?.address {
487+
addressDictionary[MBPostalAddressStreetKey] = address
492488
} else if let address = address {
493489
addressDictionary[MBPostalAddressStreetKey] = address
494490
}
@@ -519,15 +515,4 @@ open class GeocodedPlacemark: Placemark {
519515
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.
520516
*/
521517
@objc(MBQualifyingPlacemark)
522-
open class QualifyingPlacemark: Placemark {
523-
524-
@objc open override var wikidataItemIdentifier: String? {
525-
// TODO: Migrate to Codable
526-
// let item = featureJSON["wikidata"] as? String
527-
// if let item = item {
528-
// assert(item.hasPrefix("Q"))
529-
// }
530-
// return item
531-
return ""
532-
}
533-
}
518+
open class QualifyingPlacemark: Placemark {}

0 commit comments

Comments
 (0)