Swift中的反向地理编码位置 [英] Reverse Geocode Location in Swift
问题描述
我的输入是纬度和经度。我需要使用swift的 reverseGeocodeLocation
函数来给我本地的输出。我尝试使用的代码是
My input is a latitude and longitude. I need to use the reverseGeocodeLocation
function of swift, to give me the output of the locality. The code I have tried to use is
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
函数中的致命错误。我有没有把格式搞砸了?
It seems that the CLLocationCoordinate2DMake
function is failing, which then causes the fatal error in the reverseGeocodeLocation
function. Have I mucked up the format somewhere?
推荐答案
你永远不会反转地理编码的位置但你传入了manager.location。
you never reverse geocode the location but you pass in manager.location.
参见:
CLGeocoder()。reverseGeocodeLocation(manager.location,...
我认为这是一个复制和粘贴错误,这就是问题 - 代码本身看起来不错 - 差不多;)
I assume that was a copy&paste mistake and that this is the issue - the code itself looks good - almost ;)
var longitude :CLLocationDegrees = -122.0312186
var latitude :CLLocationDegrees = 37.33233141
var location = CLLocation(latitude: latitude, longitude: longitude) //changed!!!
println(location)
CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in
println(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")
}
})
这篇关于Swift中的反向地理编码位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!