As the title says, I am trying to derive the user's location from their address as they input in into the app however I have not found any way to do this. I would really appreciate it if someone could help me. Thanks!
Here is a function that you can use to get location from address
func getLocation(from address: String, completion: @escaping (_ location: CLLocationCoordinate2D?)-> Void) {
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) { (placemarks, error) in
guard let placemarks = placemarks,
let location = placemarks.first?.location?.coordinate else {
completion(nil)
return
}
completion(location)
}
}
So let's say if i have this address:
let address = "1 Infinite Loop, Cupertino, CA 95014"
Usage
getLocation(from: address) { location in
print("Location is", location.debugDescription)
// Location is Optional(__C.CLLocationCoordinate2D(latitude: 39.799372, longitude: -89.644458))
print(location.latitude) // result 39.799372
print(location.longitude) // result -89.644458
}