Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert address to coordinates swift

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.

like image 646
Paul Bénéteau Avatar asked Feb 16 '17 16:02

Paul Bénéteau


People also ask

How do I find the latitude and longitude of an address in Swift?

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?) } }

How do I change my address with latitude and longitude?

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)

Can I get coordinates from an address?

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.


2 Answers

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.

like image 100
naglerrr Avatar answered Sep 21 '22 13:09

naglerrr


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)") } 
like image 32
Greg Avatar answered Sep 23 '22 13:09

Greg