我無法返回經度和緯度。我總是得到0.0和0.0。iOS - 我怎樣才能返回經度和緯度?
我能做些什麼來獲得這些值?
代碼:
func forwardGeocoding (address: String) -> (Double, Double) {
let geoCoder = CLGeocoder()
var latitude: Double = 0.0
var longitude: Double = 0.0
geoCoder.geocodeAddressString(address) { (placemarks: [CLPlacemark]?, error: NSError?) -> Void in
if error != nil {
print(error?.localizedDescription)
} else {
if placemarks!.count > 0 {
let placemark = placemarks![0] as CLPlacemark
let location = placemark.location
latitude = Double((location?.coordinate.latitude)!)
longitude = Double((location?.coordinate.longitude)!)
print("before : \(latitude, longitude)")
}
}
}
print("after : \(latitude, longitude)")
return (latitude, longitude)
}
這是我的viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
forwardGeocoding("New York, NY, United States")
}
結果:
後:(0.0,0.0)
前:(40.713054,-74.007228)
您正在通過對geocodeAddressString異步調用獲取值之前很久纔打印並返回值。 – rmaddy