2014-10-06 117 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