【发布时间】:2015-02-14 05:09:41
【问题描述】:
我的输入是纬度和经度。我需要使用 swift 的reverseGeocodeLocation 函数,给我本地的输出。我尝试使用的代码是
println(geopoint.longitude)
println(geopoint.latitude)
var manager : CLLocationManager!
var longitude :CLLocationDegrees = geopoint.longitude
var latitude :CLLocationDegrees = geopoint.latitude
var location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
println(location)
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error) -> Void in
println(manager.location)
if error != nil {
println("Reverse geocoder failed with error" + error.localizedDescription)
return
}
if placemarks.count > 0 {
let pm = placemarks[0] as CLPlacemark
println(pm.locality)
}
else {
println("Problem with the data received from geocoder")
}
在我得到的日志中
//-122.0312186
//37.33233141
//C.CLLocationCoordinate2D
//fatal error: unexpectedly found nil while unwrapping an Optional value
CLLocationCoordinate2DMake函数似乎失败了,这导致了reverseGeocodeLocation函数中的致命错误。我是不是把格式搞砸了?
【问题讨论】:
-
您没有创建必须作为位置传递的 CLLocation。位置变量是你的情况是一个 CLLocationCoordinate2D 意思是一个坐标。您必须根据您创建的坐标创建 CLLocation 而不是 CLLocationCoordinate2D。
标签: ios swift cllocationmanager reverse-geocoding