如何制作一个 return 从地址 swift 坐标的函数 4
How to make a function that return coordinates from address swift 4
您好,我想接收地址的坐标。我做了这个功能但没有用。 Return 总是 0.0, 0.0
func getCoordinates(address: String)->(Double, Double){
DispatchQueue.main.async {
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(address) { (placemarks, error) in
guard let placemarks = placemarks, let location = placemarks.first?.location else { return }
latitude = location.coordinate.latitude
longitude = location.coordinate.longitude
}
}
return (latitude ?? 0.0, longitude ?? 0.0)
}
您需要完成并在地理编码器的回调中插入主队列块
func getCoordinates(_ address: String,completion:@escaping((CLLocationCoordinate2D) -> ())){
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(address) { (placemarks, error) in
guard let placemarks = placemarks, let location = placemarks.first?.location else { return }
DispatchQueue.main.async {
completion(location.coordinate)
}
}
}
通话
getCoordinates("someAdd") { loc in
}
您好,我想接收地址的坐标。我做了这个功能但没有用。 Return 总是 0.0, 0.0
func getCoordinates(address: String)->(Double, Double){
DispatchQueue.main.async {
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(address) { (placemarks, error) in
guard let placemarks = placemarks, let location = placemarks.first?.location else { return }
latitude = location.coordinate.latitude
longitude = location.coordinate.longitude
}
}
return (latitude ?? 0.0, longitude ?? 0.0)
}
您需要完成并在地理编码器的回调中插入主队列块
func getCoordinates(_ address: String,completion:@escaping((CLLocationCoordinate2D) -> ())){
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(address) { (placemarks, error) in
guard let placemarks = placemarks, let location = placemarks.first?.location else { return }
DispatchQueue.main.async {
completion(location.coordinate)
}
}
}
通话
getCoordinates("someAdd") { loc in
}