diff --git a/Sources/MapboxGeocoder/MBPlacemark.swift b/Sources/MapboxGeocoder/MBPlacemark.swift index efa59d4..857a995 100644 --- a/Sources/MapboxGeocoder/MBPlacemark.swift +++ b/Sources/MapboxGeocoder/MBPlacemark.swift @@ -494,21 +494,14 @@ open class GeocodedPlacemark: Placemark { } @objc open var formattedName: String { - let text = super.name - // 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. - if let houseNumber = address, scope == .address { - let streetName = text - let reversedAddress = "\(streetName) \(houseNumber)" - if qualifiedNameComponents.contains(reversedAddress) { - return reversedAddress - } else { - return "\(houseNumber) \(streetName)" - } - } else if scope == .address, precision == .intersection { - // For intersection features, `text` is just the first street name. The first line of the fully qualified address contains the cross street too. - return qualifiedNameComponents.first ?? text + guard scope == .address else { + return name + } + if precision == .intersection { + // For intersection features, `name` is just the first street name. The first line of the fully qualified address contains the cross street too. + return qualifiedNameComponents.first ?? name } else { - return text + return streetAddress ?? name } } @@ -546,6 +539,22 @@ open class GeocodedPlacemark: Placemark { return clippedAddressLines } + @objc open var streetAddress: String? { + guard scope == .address else { + return properties?.address ?? address + } + guard let address = address else { + return name + } + // For address features, `address` is a house number and `name` is just a street name. Look through the fully-qualified address to determine whether to put the house number after or before the street name (i.e. Chinese addresses). + let streetAddress = "\(name) \(address)" + if qualifiedNameComponents.contains(streetAddress) { + return streetAddress + } else { + return "\(address) \(name)" + } + } + #if canImport(Contacts) @objc open override var postalAddress: CNPostalAddress? { let postalAddress = CNMutablePostalAddress() diff --git a/Tests/MapboxGeocoderTests/ForwardGeocodingTests.swift b/Tests/MapboxGeocoderTests/ForwardGeocodingTests.swift index 554b605..db561bc 100644 --- a/Tests/MapboxGeocoderTests/ForwardGeocodingTests.swift +++ b/Tests/MapboxGeocoderTests/ForwardGeocodingTests.swift @@ -63,6 +63,7 @@ class ForwardGeocodingTests: XCTestCase { XCTAssertEqual(addressPlacemark.place?.name, "Wasaga Beach", "forward geocode should populate locality") XCTAssertEqual(addressPlacemark.thoroughfare, "Pennsylvania Ave", "forward geocode should populate thoroughfare") XCTAssertNil(addressPlacemark.subThoroughfare, "forward geocode should not populate sub-thoroughfare for street-only result") + XCTAssertEqual(addressPlacemark.streetAddress, "Pennsylvania Ave", "forward geocode should populate street address") XCTAssertNotNil(addressPlacemark.addressDictionary) let addressDictionary = addressPlacemark.addressDictionary! diff --git a/Tests/MapboxGeocoderTests/ReverseGeocodingTests.swift b/Tests/MapboxGeocoderTests/ReverseGeocodingTests.swift index 72e5f44..ff68534 100644 --- a/Tests/MapboxGeocoderTests/ReverseGeocodingTests.swift +++ b/Tests/MapboxGeocoderTests/ReverseGeocodingTests.swift @@ -60,6 +60,7 @@ class ReverseGeocodingTests: XCTestCase { XCTAssertEqual(pointOfInterestPlacemark.place?.name, "Independence", "reverse geocode should populate place") XCTAssertNil(pointOfInterestPlacemark.thoroughfare, "reverse geocode for POI should not populate thoroughfare") XCTAssertNil(pointOfInterestPlacemark.subThoroughfare, "reverse geocode for POI should not populate sub-thoroughfare") + XCTAssertEqual(pointOfInterestPlacemark.streetAddress, "2850 CR 3100", "reverse geocode for POI should populate street address") XCTAssertEqual(pointOfInterestPlacemark.wikidataItemIdentifier, "Q82112") XCTAssertNotNil(pointOfInterestPlacemark.addressDictionary) @@ -141,5 +142,6 @@ class ReverseGeocodingTests: XCTestCase { XCTAssertEqual(addressPlacemark?.name, decodedAddressPlacemark.name) XCTAssertEqual(addressPlacemark?.formattedName, decodedAddressPlacemark.formattedName) + XCTAssertEqual(addressPlacemark?.streetAddress, "850 Eldorado Street", "reverse geocode should populate street address") } }