How can I convert a String
address to CLLocation
coordinates with Swift? I have no code yet, I looked for a solution but I didn't find anyone yet.
Thanks.
geocodeAddressString(addressString) { (placemarks, error) in if error == nil { if let placemark = placemarks?[0] { let location = placemark. location! completionHandler(location. coordinate, nil) return } } completionHandler(kCLLocationCoordinate2DInvalid, error as NSError?) } }
To get the latitude of the address in cell B2, use the formula = GetLatitude(B2) To get the longitude of the address in cell B2, use the formula = GetLongitude(B2) To get both the latitude and longitude of the address in cell B2, use the formula = GetCoordinates(B2)
Get the coordinates of a placeOn your computer, open Google Maps. Right-click the place or area on the map. This will open a pop-up window. You can find your latitude and longitude in decimal format at the top.
This is pretty easy.
let address = "1 Infinite Loop, Cupertino, CA 95014" let geoCoder = CLGeocoder() geoCoder.geocodeAddressString(address) { (placemarks, error) in guard let placemarks = placemarks, let location = placemarks.first?.location else { // handle no location found return } // Use your location }
You will also need to add and import CoreLocation framework.
You can use CLGeocoder, you can convert address(string) to coordinate and you vice versa, try this:
import CoreLocation var geocoder = CLGeocoder() geocoder.geocodeAddressString("your address") { placemarks, error in let placemark = placemarks?.first let lat = placemark?.location?.coordinate.latitude let lon = placemark?.location?.coordinate.longitude print("Lat: \(lat), Lon: \(lon)") }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With