2014-10-06 121 views
5

我有一個經度/緯度位置,我想將其轉換爲Swift中的位置名稱String。做這個的最好方式是什麼?我相信最好使用reverseGeocodeLocation函數,但不完全確定如何。這是我到目前爲止有:將GPS座標轉換爲Swift中的城市名稱/地址

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { 
    if (locationFixAchieved == false) { 
     locationFixAchieved = true 
     var locationArray = locations as NSArray 
     var locationObj = locationArray.lastObject as CLLocation 
     var coord = locationObj.coordinate 
     var latitude = coord.latitude 
     var longitude = coord.longitude 

     getCurrentWeatherData("\(longitude)", latitude: "\(latitude)") 
     reverseGeocodeLocation(location:coord, completionHandler: { (<#[AnyObject]!#>, <#NSError!#>) -> Void in 
      <#code#> 
     }) 

    } 
} 

func reverseGeocodeLocation(location: CLLocation!, completionHandler: CLGeocodeCompletionHandler!){ 

} 
+0

附我所嘗試。我想我必須使用reverseGeocodeLocation函數,但不知道如何 – 2014-10-07 00:21:21

回答

11

你需要編寫的代碼是這樣的:

geocoder.reverseGeocodeLocation(currentLocation, completionHandler: { 
      placemarks, error in 

       if error == nil && placemarks.count > 0 { 
        self.placeMark = placemarks.last as? CLPlacemark 
        self.adressLabel.text = "\(self.placeMark!.thoroughfare)\n\(self.placeMark!.postalCode) \(self.placeMark!.locality)\n\(self.placeMark!.country)" 
        self.manager.stopUpdatingLocation() 
       } 
      }) 
+1

我只需添加附加所有地標組件的intead,您可以使用:self.adressLabel.text = ABCreateStringWithAddressDictionary(self.placemark.addressDictionary!,false)for <=來自AdressBookUI框架的iOS 8和聯繫人框架中的CNPostaAddressFormatter> iOS 9.0 – 2015-10-08 20:52:32

+0

謝謝,我不知道! – Ben 2015-10-09 19:41:22

4

請查看這個答案。

func getAddressFromGeocodeCoordinate(coordinate: CLLocationCoordinate2D) { 
    let geocoder = GMSGeocoder() 
    geocoder.reverseGeocodeCoordinate(coordinate) { response , error in 

     //Add this line 
     if let address = response!.firstResult() { 
     let lines = address.lines! as [String] 
     print(lines) 

     } 
    } 
    } 
0

斯威夫特代碼來獲得地址的標的,很好地格式化

let address = ABCreateStringWithAddressDictionary(placemark.addressDictionary!, false).componentsSeparatedByString("\n").joinWithSeparator(", ") 

print(address!) 
3

我使用的斯威夫特3/XCode的8

我用Benjamin Herzog's answer但我遇到了一些相關的構建錯誤到可選和鑄造。

在重寫它時,我決定將它封裝在一個函數中並進行概括,以便它可以很容易地插入到任何地方。

import CoreLocation 

func getPlacemark(forLocation location: CLLocation, completionHandler: @escaping (CLPlacemark?, String?) ->()) { 
    let geocoder = CLGeocoder() 

    geocoder.reverseGeocodeLocation(location, completionHandler: { 
     placemarks, error in 

     if let err = error { 
      completionHandler(nil, err.localizedDescription) 
     } else if let placemarkArray = placemarks { 
      if let placemark = placemarkArray.first { 
       completionHandler(placemark, nil) 
      } else { 
       completionHandler(nil, "Placemark was nil") 
      } 
     } else { 
      completionHandler(nil, "Unknown error") 
     } 
    }) 

} 

使用它:

getPlacemark(forLocation: originLocation) { 
    (originPlacemark, error) in 
     if let err = error { 
      print(err) 
     } else if let placemark = originPlacemark { 
      // Do something with the placemark 
     } 
    }) 
} 
+0

好男人!乾杯 – 2017-05-04 07:29:56