Skip to content

adding convenience streetAddress property #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions Sources/MapboxGeocoder/MBPlacemark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions Tests/MapboxGeocoderTests/ForwardGeocodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
2 changes: 2 additions & 0 deletions Tests/MapboxGeocoderTests/ReverseGeocodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}
}