|
| 1 | +import Foundation |
| 2 | + |
| 3 | + |
| 4 | +/** |
| 5 | + A concrete subclass of `Placemark` to represent results of geocoding requests. |
| 6 | + */ |
| 7 | +@objc(MBGeocodedPlacemark) |
| 8 | +open class GeocodedPlacemark: Placemark { |
| 9 | + |
| 10 | + private enum CodingKeys: String, CodingKey { |
| 11 | + case routableLocations = "routable_points" |
| 12 | + case relevance |
| 13 | + } |
| 14 | + |
| 15 | + private enum PointsCodingKeys: String, CodingKey { |
| 16 | + case points |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + An array of locations that serve as hints for navigating to the placemark. |
| 21 | + |
| 22 | + If the `GeocodeOptions.includesRoutableLocations` property is set to `true`, this property contains locations that are suitable to use as a waypoint in a routing engine such as MapboxDirections.swift. Otherwise, if the `GeocodeOptions.includesRoutableLocations` property is set to `false`, this property is set to `nil`. |
| 23 | + |
| 24 | + For the placemark’s geographic center, use the `location` property. The routable locations may differ from the geographic center. For example, if a house’s driveway leads to a street other than the nearest street (by straight-line distance), then this property may contain the location where the driveway meets the street. A route to the placemark’s geographic center may be impassable, but a route to the routable location would end on the correct street with access to the house. |
| 25 | + */ |
| 26 | + @objc open var routableLocations: [CLLocation]? |
| 27 | + |
| 28 | + public required init(from decoder: Decoder) throws { |
| 29 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 30 | + |
| 31 | + if let pointsContainer = try? container.nestedContainer(keyedBy: PointsCodingKeys.self, forKey: .routableLocations), |
| 32 | + var coordinatesContainer = try? pointsContainer.nestedUnkeyedContainer(forKey: .points) { |
| 33 | + |
| 34 | + if let routableLocation = try coordinatesContainer.decodeIfPresent(RoutableLocation.self), |
| 35 | + let coordinate = routableLocation.coordinate { |
| 36 | + routableLocations = [CLLocation(coordinate: coordinate)] |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + relevance = try container.decodeIfPresent(Double.self, forKey: .relevance) ?? -1 |
| 41 | + |
| 42 | + try super.init(from: decoder) |
| 43 | + } |
| 44 | + |
| 45 | + public override func encode(to encoder: Encoder) throws { |
| 46 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 47 | + |
| 48 | + try container.encodeIfPresent(relevance, forKey: .relevance) |
| 49 | + |
| 50 | + if let routableLocations = routableLocations, |
| 51 | + !routableLocations.isEmpty { |
| 52 | + var pointsContainer = container.nestedContainer(keyedBy: PointsCodingKeys.self, forKey: .routableLocations) |
| 53 | + var coordinatesContainer = pointsContainer.nestedUnkeyedContainer(forKey: .points) |
| 54 | + let routableLocation = RoutableLocation(coordinates: [routableLocations[0].coordinate.longitude, |
| 55 | + routableLocations[0].coordinate.latitude]) |
| 56 | + try coordinatesContainer.encode(routableLocation) |
| 57 | + } |
| 58 | + |
| 59 | + try super.encode(to: encoder) |
| 60 | + } |
| 61 | + |
| 62 | + @objc open override var debugDescription: String { |
| 63 | + return qualifiedName! |
| 64 | + } |
| 65 | + |
| 66 | + internal var qualifiedNameComponents: [String] { |
| 67 | + if qualifiedName!.contains(", ") { |
| 68 | + return qualifiedName!.components(separatedBy: ", ") |
| 69 | + } |
| 70 | + // Chinese addresses have no commas and are reversed. |
| 71 | + return (superiorPlacemarks?.map { $0.name } ?? []).reversed() + [name] |
| 72 | + } |
| 73 | + |
| 74 | + @objc open var formattedName: String { |
| 75 | + let text = super.name |
| 76 | + // 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. |
| 77 | + if let houseNumber = address, scope == .address { |
| 78 | + let streetName = text |
| 79 | + let reversedAddress = "\(streetName) \(houseNumber)" |
| 80 | + if qualifiedNameComponents.contains(reversedAddress) { |
| 81 | + return reversedAddress |
| 82 | + } else { |
| 83 | + return "\(houseNumber) \(streetName)" |
| 84 | + } |
| 85 | + } else { |
| 86 | + return text |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @objc open override var genres: [String]? { |
| 91 | + return properties?.category?.components(separatedBy: ", ") |
| 92 | + } |
| 93 | + |
| 94 | + @objc open override var imageName: String? { |
| 95 | + return properties?.maki |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + A numerical score from 0 (least relevant) to 0.99 (most relevant) measuring |
| 100 | + how well each returned feature matches the query. Use this property to |
| 101 | + remove results that don’t fully match the query. |
| 102 | + */ |
| 103 | + @objc open var relevance: Double |
| 104 | + |
| 105 | + private var clippedAddressLines: [String] { |
| 106 | + let lines = qualifiedNameComponents |
| 107 | + if scope == .address { |
| 108 | + return lines |
| 109 | + } |
| 110 | + |
| 111 | + guard let qualifiedName = qualifiedName, |
| 112 | + qualifiedName.contains(", ") else { |
| 113 | + // Chinese addresses have no commas and are reversed. |
| 114 | + return Array(lines.prefix(lines.count)) |
| 115 | + } |
| 116 | + |
| 117 | + return Array(lines.suffix(from: 1)) |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + The placemark’s full address in the customary local format, with each line in a separate string in the array. |
| 122 | + |
| 123 | + If you need to fit the same address on a single line, use the `qualifiedName` property, in which each line is separated by a comma instead of a line break. |
| 124 | + */ |
| 125 | + var formattedAddressLines: [String]? { |
| 126 | + return clippedAddressLines |
| 127 | + } |
| 128 | + |
| 129 | + #if !os(tvOS) |
| 130 | + @available(iOS 9.0, OSX 10.11, *) |
| 131 | + @objc open override var postalAddress: CNPostalAddress? { |
| 132 | + let postalAddress = CNMutablePostalAddress() |
| 133 | + |
| 134 | + if scope == .address { |
| 135 | + postalAddress.street = name |
| 136 | + } else if let address = address { |
| 137 | + postalAddress.street = address.replacingOccurrences(of: ", ", with: "\n") |
| 138 | + } |
| 139 | + |
| 140 | + if let placeName = place?.name { |
| 141 | + postalAddress.city = placeName |
| 142 | + } |
| 143 | + if let regionName = administrativeRegion?.name { |
| 144 | + postalAddress.state = regionName |
| 145 | + } |
| 146 | + if let postalCode = postalCode?.name { |
| 147 | + postalAddress.postalCode = postalCode |
| 148 | + } |
| 149 | + if let countryName = country?.name { |
| 150 | + postalAddress.country = countryName |
| 151 | + } |
| 152 | + if let ISOCountryCode = country?.code { |
| 153 | + postalAddress.isoCountryCode = ISOCountryCode |
| 154 | + } |
| 155 | + |
| 156 | + return postalAddress |
| 157 | + } |
| 158 | + #endif |
| 159 | + |
| 160 | + open override var code: String? { |
| 161 | + get { |
| 162 | + return country?.code |
| 163 | + } |
| 164 | + set { |
| 165 | + country?.code = code |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + @objc open override var addressDictionary: [AnyHashable: Any]? { |
| 170 | + var addressDictionary: [String: Any] = [:] |
| 171 | + if scope == .address { |
| 172 | + addressDictionary[MBPostalAddressStreetKey] = name |
| 173 | + } else if let address = properties?.address { |
| 174 | + addressDictionary[MBPostalAddressStreetKey] = address |
| 175 | + } else if let address = address { |
| 176 | + addressDictionary[MBPostalAddressStreetKey] = address |
| 177 | + } |
| 178 | + addressDictionary[MBPostalAddressCityKey] = place?.name |
| 179 | + addressDictionary[MBPostalAddressStateKey] = administrativeRegion?.name |
| 180 | + addressDictionary[MBPostalAddressPostalCodeKey] = postalCode?.name |
| 181 | + addressDictionary[MBPostalAddressCountryKey] = country?.name |
| 182 | + addressDictionary[MBPostalAddressISOCountryCodeKey] = country?.code |
| 183 | + addressDictionary["formattedAddressLines"] = clippedAddressLines |
| 184 | + addressDictionary["name"] = name |
| 185 | + addressDictionary["subAdministrativeArea"] = district?.name ?? place?.name |
| 186 | + addressDictionary["subLocality"] = neighborhood?.name |
| 187 | + addressDictionary["subThoroughfare"] = subThoroughfare |
| 188 | + addressDictionary["thoroughfare"] = thoroughfare |
| 189 | + return addressDictionary |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + The phone number to contact a business at this location. |
| 194 | + */ |
| 195 | + @objc open override var phoneNumber: String? { |
| 196 | + return properties?.phoneNumber |
| 197 | + } |
| 198 | +} |
0 commit comments