6

Im trying to convert CLLocationCoordinates2d coordinates to a physical street address.

I have tried CLGeocoder with no luck. Here is my code that supplies the latitude and longitude coordinates.

    let manager = CLLocationManager()

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{

    guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
    print("locations = \(locValue.latitude) \(locValue.longitude)")
    // let locationtext = "locations = \(locValue.latitude) \(locValue.longitude)"
    locationTxt.text = "\(locValue.latitude) \(locValue.longitude)"

}

Thank you!

klykins
  • 95
  • 1
  • 6

2 Answers2

8
func convertLatLongToAddress(latitude:Double,longitude:Double){
    
    let geoCoder = CLGeocoder()
    let location = CLLocation(latitude: latitude, longitude: longitude)
    geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
        
        // Place details
        var placeMark: CLPlacemark!
        placeMark = placemarks?[0]
        
        // Location name
        if let locationName = placeMark.location {
            print(locationName)
        }
        // Street address
        if let street = placeMark.thoroughfare {
            print(street)
        }
        // City
        if let city = placeMark.locality {
            print(city)
        }
        // State
        if let state = placemark.administrativeArea {
            print(state)
        }
        // Zip code
        if let zipCode = placeMark.postalCode {
            print(zipCode)
        }
        // Country
        if let country = placeMark.country {
            print(country)
        }
    })
    
}
Beltalowda
  • 4,558
  • 2
  • 25
  • 33
Mahesh Dangar
  • 806
  • 5
  • 11
  • thanks for the fast reply! so im getting errors on geocoder and location. geoCoder CLGeocoder 0x00006040000171e0 and location CLLocation 0x00006040000170f0 – klykins Aug 18 '18 at 06:37
  • @klykins which error and on which line you getting error in my code let me know so i will check it again – Mahesh Dangar Aug 18 '18 at 06:38
  • line 1 and 2 do i need to import anything else? import MapKit import CoreLocation – klykins Aug 18 '18 at 06:42
  • @klykins yes ofCourse u need to import CoreLocation – Mahesh Dangar Aug 18 '18 at 06:44
  • i have that being imported currently – klykins Aug 18 '18 at 06:45
  • @klykins i have updated my answer kindly check it again and just put that function in your code and call it where you need by passing latitude and longitude – Mahesh Dangar Aug 18 '18 at 06:53
  • @klykins and i have tested it and it's working fine for me – Mahesh Dangar Aug 18 '18 at 06:53
  • the if let zip = placeMark.isoCountryCode is misleading. isoCountryCode according to Apple Docs is the countries abbreviated name such as "UK" or "US" not the postal code. That property is `postalCode`. Also, the city isn't the subAdministrativeArea is the county that `could contain` a city. Subadministrative areas typically correspond to counties or other regions that are then organized into a larger administrative area or state. – TimD Aug 11 '20 at 17:47
3

Yes you can get street address from Coordinates for that you need following code

func getAddressFromLatLon(Latitude: Double, Longitude: Double) {
    var center : CLLocationCoordinate2D = CLLocationCoordinate2D()


    let ceo: CLGeocoder = CLGeocoder()
    center.latitude = Latitude
    center.longitude = Longitude

    let loc: CLLocation = CLLocation(latitude:center.latitude, longitude: center.longitude)


    ceo.reverseGeocodeLocation(loc, completionHandler:
        {(placemarks, error) in
            if (error != nil)
            {
                print("reverse geodcode fail: \(error!.localizedDescription)")
            }
            let pm = placemarks! as [CLPlacemark]

            if pm.count > 0 {
                let pm = placemarks![0]
                print(pm.country)
                print(pm.locality)
                print(pm.subLocality)
                print(pm.thoroughfare)
                print(pm.postalCode)
                print(pm.subThoroughfare)
                var addressString : String = ""
                if pm.subLocality != nil {
                    addressString = addressString + pm.subLocality! + ", "
                }
                if pm.thoroughfare != nil {
                    addressString = addressString + pm.thoroughfare! + ", "
                }
                if pm.locality != nil {
                    addressString = addressString + pm.locality! + ", "
                }
                if pm.country != nil {
                    addressString = addressString + pm.country! + ", "
                }
                if pm.postalCode != nil {
                    addressString = addressString + pm.postalCode! + " "
                }


                print(addressString)
          }
    })

}

Call this function and pass parameter you can get address string. if not works then comment so i guide you further.

Kiran Sarvaiya
  • 1,318
  • 1
  • 13
  • 37