2016-07-29 36 views
2

下面的代碼怎麼辦:您閱讀使用coreLocation(的iOS/SWIFT)詳細地址

CLGeocoder().reverseGeocodeLocation(myCurrentLoc) { (array, err) in 

     print(err) 
     if err != nil { 
      print(err) 
      return 
     } 

     if let place = array?.first { 

      if let locality = place.locality { 
       if let country = place.country { 
        if let addr = place.addressDictionary { 
         print(addr) 
        } 
        let myAddress = "\(locality) \(country)" 
        self.adress.text = myAddress 
        self.locMgr.stopUpdatingLocation() 

       } 
      } 

     } 
     self.locMgr.stopUpdatingLocation() 

    } 

所以我可以讀/緯度,而我唯一需要的是完整的地址。現在我可以打印出「紐約,美國」,我發現我需要的所有信息都在place.addressDictionary中,這是一本字典。但我似乎無法使用正常語法從該字典中讀取任何內容。我該怎麼做?

回答

0

我通常做一個位置管理助手獲取詳細信息,並使用標

你所談論的字典帶有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") 
     } 
} 

你也可以像

enter image description here

enter image description here

獲取標的的其它特性我還印製了addressDictionary,您可以在最後的圖片中看到控制檯的完整地址

0

對於美國地址,您可以使用:

let address = [ 
       place.name, 
       place.locality, 
       place.administrativeArea, 
       place.postalCode, 
       place.country 
] 

let formattedAddress = address.flatMap({ $0 }).joined(separator: ", ") 

Address是用於組成典型郵政地址的組件數組。它可以包含一些項目的零。

FormattedAddress刪除nils並添加分隔符,我在這裏使用昏迷。

+0

類型[String]的值沒有「加入」的成員 –

+0

@FateRiddle它在Swift 3中執行。如果使用較舊,則有'joinWithSeparator' – MirekE

相關問題