Skip to content

Commit 51afbb6

Browse files
committed
adding convenience streetAddress property
1 parent f6b6b87 commit 51afbb6

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

MapboxGeocoder/MBPlacemark.swift

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -488,21 +488,14 @@ open class GeocodedPlacemark: Placemark {
488488
}
489489

490490
@objc open var formattedName: String {
491-
let text = super.name
492-
// 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.
493-
if let houseNumber = address, scope == .address {
494-
let streetName = text
495-
let reversedAddress = "\(streetName) \(houseNumber)"
496-
if qualifiedNameComponents.contains(reversedAddress) {
497-
return reversedAddress
498-
} else {
499-
return "\(houseNumber) \(streetName)"
500-
}
501-
} else if scope == .address, precision == .intersection {
502-
// For intersection features, `text` is just the first street name. The first line of the fully qualified address contains the cross street too.
503-
return qualifiedNameComponents.first ?? text
491+
guard scope == .address else {
492+
return name
493+
}
494+
if precision == .intersection {
495+
// For intersection features, `name` is just the first street name. The first line of the fully qualified address contains the cross street too.
496+
return qualifiedNameComponents.first ?? name
504497
} else {
505-
return text
498+
return streetAddress ?? name
506499
}
507500
}
508501

@@ -540,6 +533,22 @@ open class GeocodedPlacemark: Placemark {
540533
return clippedAddressLines
541534
}
542535

536+
@objc open var streetAddress: String? {
537+
guard scope == .address else {
538+
return properties?.address ?? address
539+
}
540+
guard let address = address else {
541+
return name
542+
}
543+
// 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).
544+
let streetAddress = "\(name) \(address)"
545+
if qualifiedNameComponents.contains(streetAddress) {
546+
return streetAddress
547+
} else {
548+
return "\(address) \(name)"
549+
}
550+
}
551+
543552
#if !os(tvOS)
544553
@available(iOS 9.0, OSX 10.11, *)
545554
@objc open override var postalAddress: CNPostalAddress? {

0 commit comments

Comments
 (0)