Last active
March 28, 2019 19:02
-
-
Save SergLam/44305a41ff143d47688710121d8991a7 to your computer and use it in GitHub Desktop.
MapKit full location address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import MapKit | |
import CoreLocation | |
import Foundation | |
extension MKMapItem { | |
var fullName: String { | |
guard let name = name, !placemark.fullLocationAddress.contains(name) else { | |
return placemark.fullLocationAddress | |
} | |
return (name + ", ") + placemark.fullLocationAddress | |
} | |
} | |
extension CLPlacemark { | |
var fullLocationAddress: String { | |
// MARK: Get the same address, that could be provided by Google Places API | |
// https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete | |
var placemarkData: [String] = [] | |
if let area = areasOfInterest?.first { placemarkData.append(area.localizedCapitalized) } | |
if let street = thoroughfare?.localizedCapitalized { placemarkData.append(street) } | |
if let building = subThoroughfare?.localizedCapitalized { placemarkData.append(building)} | |
if let city = locality?.localizedCapitalized { placemarkData.append(city) } | |
if let subCity = subLocality?.localizedCapitalized { placemarkData.append(subCity) } | |
if let state = administrativeArea?.localizedCapitalized { placemarkData.append(state) } | |
if let stateArea = subAdministrativeArea?.localizedCapitalized { placemarkData.append(stateArea) } | |
if let county = country?.localizedCapitalized { placemarkData.append(county) } | |
placemarkData.removeDuplicates() | |
var result = "" | |
placemarkData.forEach { result.append(" "+$0+",") } | |
result = result.trimmed | |
result.removeLast() // remove last comma | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment