Skip to content

Commit eacb927

Browse files
committed
Codable region, category, name, address
1 parent 910eed2 commit eacb927

File tree

2 files changed

+50
-54
lines changed

2 files changed

+50
-54
lines changed

MapboxGeocoder/MBPlacemark.swift

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,14 @@ open class Placemark: NSObject, Codable {
6868
private enum CodingKeys: String, CodingKey {
6969
case identifier = "id"
7070
case name = "text"
71-
case address = "address"
71+
case address
7272
case qualifiedName = "place_name"
7373
case superiorPlacemarks = "context"
74-
case center = "center"
74+
case center
7575
case code = "short_code"
7676
case wikidataItemIdentifier = "wikidata"
77-
case properties = "properties"
77+
case properties
78+
case boundingBox = "bbox"
7879
}
7980

8081
/**
@@ -99,6 +100,12 @@ open class Placemark: NSObject, Codable {
99100
}
100101

101102
properties = try container.decodeIfPresent(Properties.self, forKey: .properties)
103+
104+
if let boundingBox = try container.decodeIfPresent([CLLocationDegrees].self, forKey: .boundingBox) {
105+
let southWest = CLLocationCoordinate2D(geoJSON: Array(boundingBox.prefix(2)))
106+
let northEast = CLLocationCoordinate2D(geoJSON: Array(boundingBox.suffix(2)))
107+
region = RectangularRegion(southWest: southWest, northEast: northEast)
108+
}
102109
}
103110

104111
public func encode(to encoder: Encoder) throws {
@@ -214,9 +221,7 @@ open class Placemark: NSObject, Codable {
214221

215222
When this property is not `nil`, it is currently always a `RectangularRegion`. In the future, it may be another type of `CLRegion`.
216223
*/
217-
@objc open var region: CLRegion? {
218-
return nil
219-
}
224+
@objc open var region: CLRegion?
220225

221226
// MARK: Accessing Contact Information
222227

@@ -363,11 +368,13 @@ struct Properties: Codable {
363368
case phoneNumber = "tel"
364369
case maki
365370
case address
371+
case category
366372
}
367373
let shortCode: String?
368374
let maki: String?
369375
let phoneNumber: String?
370376
let address: String?
377+
let category: String?
371378
}
372379

373380
/**
@@ -388,60 +395,47 @@ open class GeocodedPlacemark: Placemark {
388395
return (superiorPlacemarks?.map { $0.name } ?? []).reversed() + [name]
389396
}
390397

391-
@objc open override var region: CLRegion? {
392-
// TODO: Fix!
393-
// guard let boundingBox = featureJSON["bbox"] as? [Double] else {
394-
// return nil
395-
// }
396-
//
397-
// assert(boundingBox.count == 4)
398-
// let southWest = CLLocationCoordinate2D(geoJSON: Array(boundingBox.prefix(2)))
399-
// let northEast = CLLocationCoordinate2D(geoJSON: Array(boundingBox.suffix(2)))
400-
let s = CLLocationCoordinate2D(latitude: 0, longitude: 0)
401-
return RectangularRegion(southWest: s, northEast: s)
402-
}
403-
404-
// @objc open override var name: String {
405-
// let text = super.name
406-
//
407-
// // For address features, `text` is just the street name. Look through the fully-qualified address to determine whether to put the house number before or after the street name.
408-
// if let houseNumber = featureJSON["address"] as? String, scope == .address {
409-
// let streetName = text
410-
// let reversedAddress = "\(streetName) \(houseNumber)"
411-
// if qualifiedNameComponents.contains(reversedAddress) {
412-
// return reversedAddress
413-
// } else {
414-
// return "\(houseNumber) \(streetName)"
415-
// }
416-
// } else {
417-
// return text
418-
// }
419-
// }
398+
@objc open override var name: String {
399+
get {
400+
let text = super.name
401+
// For address features, `text` is just the street name. Look through the fully-qualified address to determine whether to put the house number before or after the street name.
402+
if let houseNumber = self.address, scope == .address {
403+
let streetName = text
404+
let reversedAddress = "\(streetName) \(houseNumber)"
405+
if qualifiedNameComponents.contains(reversedAddress) {
406+
return reversedAddress
407+
} else {
408+
return "\(houseNumber) \(streetName)"
409+
}
410+
} else {
411+
return text
412+
}
413+
} set {
414+
super.name = name
415+
}
416+
}
420417

421418
@objc open override var genres: [String]? {
422-
// TODO: Fix
423-
// let categoryList = propertiesJSON["category"] as? String
424-
// return categoryList?.components(separatedBy: ", ")
425-
return [""]
419+
return properties?.category?.components(separatedBy: ", ")
426420
}
427421

428422
@objc open override var imageName: String? {
429-
// TODO: Fix
430-
//return propertiesJSON["maki"] as? String
431-
return ""
423+
return properties?.maki
432424
}
433425

434426
private var clippedAddressLines: [String] {
435-
// let lines = qualifiedNameComponents
436-
// if scope == .address {
437-
// return lines
438-
// }
439-
// guard qualifiedName.contains(", ") else {
440-
// // Chinese addresses have no commas and are reversed.
441-
// return Array(lines.prefix(lines.count))
442-
// }
443-
// return Array(lines.suffix(from: 1))
444-
return [""]
427+
let lines = qualifiedNameComponents
428+
if scope == .address {
429+
return lines
430+
}
431+
432+
guard let qualifiedName = qualifiedName,
433+
qualifiedName.contains(", ") else {
434+
// Chinese addresses have no commas and are reversed.
435+
return Array(lines.prefix(lines.count))
436+
}
437+
438+
return Array(lines.suffix(from: 1))
445439
}
446440

447441
override var formattedAddressLines: [String] {
@@ -506,8 +500,7 @@ open class GeocodedPlacemark: Placemark {
506500
The phone number to contact a business at this location.
507501
*/
508502
@objc open override var phoneNumber: String? {
509-
//return propertiesJSON["tel"] as? String
510-
return ""
503+
return properties?.phoneNumber
511504
}
512505
}
513506

MapboxGeocoderTests/ReverseGeocodingTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class ReverseGeocodingTests: XCTestCase {
4343
XCTAssertEqual(pointOfInterestPlacemark.name, "Jones Jerry", "reverse geocode should populate name")
4444
XCTAssertEqual(pointOfInterestPlacemark.qualifiedNameComponents, ["Jones Jerry", "2850 CR 3100", "Independence", "Kansas 67301", "United States"], "reverse geocode should populate qualified name")
4545
XCTAssertEqual(pointOfInterestPlacemark.qualifiedName, "Jones Jerry, 2850 CR 3100, Independence, Kansas 67301, United States", "reverse geocode should populate qualified name")
46+
XCTAssertEqual(pointOfInterestPlacemark.genres!, ["legal", "lawyer", "law", "law office"])
47+
XCTAssertEqual(pointOfInterestPlacemark.phoneNumber!, "(620) 289-4288")
48+
XCTAssertEqual(pointOfInterestPlacemark.imageName!, "suitcase")
4649
XCTAssertEqual(pointOfInterestPlacemark.location!.coordinate.latitude, 37.128003, "reverse geocode should populate location")
4750
XCTAssertEqual(pointOfInterestPlacemark.location!.coordinate.longitude, -95.782951, "reverse geocode should populate location")
4851
XCTAssertEqual(pointOfInterestPlacemark.scope, PlacemarkScope.pointOfInterest, "reverse geocode should populate scope")

0 commit comments

Comments
 (0)