Swift 中的 CLGeocoder - 使用 reverseGeocodeLocation 时无法 return 字符串
CLGeocoder in Swift - unable to return string when using reverseGeocodeLocation
我正在尝试使用 CLGeocoder return 字符串中坐标的位置。我的代码目前看起来像这样:
func getPlaceName(latitude: Double, longitude: Double) -> String {
let coordinates = CLLocation(latitude: latitude, longitude: longitude)
var answer = ""
CLGeocoder().reverseGeocodeLocation(coordinates, completionHandler: {(placemarks, error) -> Void in
if (error != nil) {
println("Reverse geocoder failed with an error" + error.localizedDescription)
answer = ""
}
if placemarks.count > 0 {
let pm = placemarks[0] as CLPlacemark
answer = displayLocationInfo(pm)
} else {
println("Problems with the data received from geocoder.")
answer = ""
}
})
return answer
}
func displayLocationInfo(placemark: CLPlacemark?) -> String
{
if let containsPlacemark = placemark
{
let locality = (containsPlacemark.locality != nil) ? containsPlacemark.locality : ""
let postalCode = (containsPlacemark.postalCode != nil) ? containsPlacemark.postalCode : ""
let administrativeArea = (containsPlacemark.administrativeArea != nil) ? containsPlacemark.administrativeArea : ""
let country = (containsPlacemark.country != nil) ? containsPlacemark.country : ""
println(locality)
println(postalCode)
println(administrativeArea)
println(country)
return locality
} else {
return ""
}
}
除了能够 return 来自 getPlaceNames() 的字符串外,一切似乎都正常。我只得到以下 returned:
Optional("")
displayLocationInfo() 函数似乎工作正常,因为 println() 输出正常。所以我相信 getPlaceName() 函数确实是从 displayLocationInfo() 获取位置字符串。
有什么想法吗?谢谢
由于 reverseGeocodeLocation
是一个异步函数,您需要让 getPlaceName
函数通过块而不是 return 语句传回答案。示例:
func getPlaceName(latitude: Double, longitude: Double, completion: (answer: String?) -> Void) {
let coordinates = CLLocation(latitude: latitude, longitude: longitude)
CLGeocoder().reverseGeocodeLocation(coordinates, completionHandler: {(placemarks, error) -> Void in
if (error != nil) {
println("Reverse geocoder failed with an error" + error.localizedDescription)
completion(answer: "")
} else if placemarks.count > 0 {
let pm = placemarks[0] as CLPlacemark
completion(answer: displayLocaitonInfo(pm))
} else {
println("Problems with the data received from geocoder.")
completion(answer: "")
}
})
}
我正在尝试使用 CLGeocoder return 字符串中坐标的位置。我的代码目前看起来像这样:
func getPlaceName(latitude: Double, longitude: Double) -> String {
let coordinates = CLLocation(latitude: latitude, longitude: longitude)
var answer = ""
CLGeocoder().reverseGeocodeLocation(coordinates, completionHandler: {(placemarks, error) -> Void in
if (error != nil) {
println("Reverse geocoder failed with an error" + error.localizedDescription)
answer = ""
}
if placemarks.count > 0 {
let pm = placemarks[0] as CLPlacemark
answer = displayLocationInfo(pm)
} else {
println("Problems with the data received from geocoder.")
answer = ""
}
})
return answer
}
func displayLocationInfo(placemark: CLPlacemark?) -> String
{
if let containsPlacemark = placemark
{
let locality = (containsPlacemark.locality != nil) ? containsPlacemark.locality : ""
let postalCode = (containsPlacemark.postalCode != nil) ? containsPlacemark.postalCode : ""
let administrativeArea = (containsPlacemark.administrativeArea != nil) ? containsPlacemark.administrativeArea : ""
let country = (containsPlacemark.country != nil) ? containsPlacemark.country : ""
println(locality)
println(postalCode)
println(administrativeArea)
println(country)
return locality
} else {
return ""
}
}
除了能够 return 来自 getPlaceNames() 的字符串外,一切似乎都正常。我只得到以下 returned:
Optional("")
displayLocationInfo() 函数似乎工作正常,因为 println() 输出正常。所以我相信 getPlaceName() 函数确实是从 displayLocationInfo() 获取位置字符串。
有什么想法吗?谢谢
由于 reverseGeocodeLocation
是一个异步函数,您需要让 getPlaceName
函数通过块而不是 return 语句传回答案。示例:
func getPlaceName(latitude: Double, longitude: Double, completion: (answer: String?) -> Void) {
let coordinates = CLLocation(latitude: latitude, longitude: longitude)
CLGeocoder().reverseGeocodeLocation(coordinates, completionHandler: {(placemarks, error) -> Void in
if (error != nil) {
println("Reverse geocoder failed with an error" + error.localizedDescription)
completion(answer: "")
} else if placemarks.count > 0 {
let pm = placemarks[0] as CLPlacemark
completion(answer: displayLocaitonInfo(pm))
} else {
println("Problems with the data received from geocoder.")
completion(answer: "")
}
})
}