"Your other post mentions you have an equivalent getLongFromAddress method. That’s not a good idea if you actually need both coordinates, since both call geocodeAddressFromString which is async and relatively slow. Instead you should have a single (say) getLocationFromAddress to get both the latitude and longitude together. If in any any specific call you need only one of them, then that’s fine."
thx you @Claude31@Scott for yours respond and just below i make my function get the lat and the long at same time.
i have another question can you help me to make my function to transform into a async await plz or send me a good tuto
func getLatLongFromAddress(withAddress address: String, completionHandler: @escaping (CLLocationDegrees,CLLocationDegrees) -> Void) {
let geocoder = CLGeocoder()
// Use CLGeocoder to convert the address into coordinates
geocoder.geocodeAddressString(address) { (placemarks, error) in
// Return early if there was an error
guard error == nil else {
return
}
// Return early if no placemarks were found
guard let placemarks = placemarks, !placemarks.isEmpty else {
return
}
// Use the first placemark to obtain the coordinates
let location = placemarks.first!.location
let lat = location!.coordinate.latitude
let long = location!.coordinate.longitude
print("lat : ",lat)
print("long : ",long)
completionHandler(lat,long)
}
}
on my main :
getLatLongFromAddress(withAddress: address1) { (lat,long) in
lat1 = lat
long1 = long
print("when i call it : lat1 : ",lat1," long1 : ",long1)
}
getLatLongFromAddress(withAddress: address2) { (lat,long) in
lat1 = lat
long1 = long
print("when i call it : lat1 : ",lat2," long1 : ",long2)
}