我通常做一個位置管理助手獲取詳細信息,並使用標
你所談論的字典帶有placemarks
這裏是我的模型
class UserLocation: NSObject {
var latitude : String?
var longitude: String?
var timeZone: String?
var city : String?
var state : String?
var country : String?
// you can and more fields of placemark
required init(latitude:String?,longitude:String?,timezone:String?,city:String?,state:String?,country:String?) {
self.latitude = latitude
self.longitude = longitude
self.timeZone = timezone
self.city = city
self.state = state
self.country = country
}
}
然後我用以下代碼在從位置管理器獲取位置後反轉地理代碼並使用placemarks
獲取詳細信息
//Location Manager delegate
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
manager.stopUpdatingLocation()
CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in
if (error != nil) {
print("Reverse geocoder failed with error" + error!.localizedDescription)
return
}
if placemarks!.count > 0 {
let pm = placemarks![0]
if let location = manager.location {
let userLoc = UserLocation(latitude: String(location.coordinate.latitude), longitude: String(location.coordinate.longitude), timezone: NSTimeZone.localTimeZone().name, city: pm.locality!, state: pm.administrativeArea!, country:pm.country!)
//By printing this dictionary you will get the complete address
print(pm.addressDictionary)
print(userLoc.city!)
print(userLoc.state!)
print(userLoc.country!)
print(userLoc.latitude!)
print(userLoc.longitude!)
print(userLoc.timeZone!)
} else {
//Handle error
}
if(!CLGeocoder().geocoding){
CLGeocoder().cancelGeocode()
}
}else{
print("Problem with the data received from geocoder")
}
}
你也可以像
獲取標的的其它特性我還印製了addressDictionary,您可以在最後的圖片中看到控制檯的完整地址
類型[String]的值沒有「加入」的成員 –
@FateRiddle它在Swift 3中執行。如果使用較舊,則有'joinWithSeparator' – MirekE