Search code examples
iosswiftcllocationcllocationcoordinate2d

How to get the address from the coordinate


I want to get the address from the coordinate. I have attached my code below..

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let lastLocation = locations.last!
    let latvalue     = lastLocation.coordinate.latitude
    let lngvalue     = lastLocation.coordinate.longitude
    self.db_latvalue = latvalue
    self.db_lngvalue = lngvalue
    let location = CLLocation(latitude: latvalue, longitude:lngvalue)
    let address = CLGeocoder.init()
    address.reverseGeocodeLocation(CLLocation.init(latitude: latvalue, longitude:lngvalue)) { (places, error) in
        if error == nil{
            if let place = places{
                   print("addressshowingssq \(place)")
                self.db_address = "\(place)"

            }
        }
    }

Output:

[L-30 2nd A Main Road, L-30 2nd A Main Road, HSR Layout, Bengaluru, Karnataka 560102, India @ <+12.91597974,+77.62879254> +/- 100.00m, region CLCircularRegion (identifier:'<+12.91597974,+77.62879254> radius 70.94', center:<+12.91597974,+77.62879254>, radius:70.94m)]

I want only the address as i mention below

L-30 2nd A Main Road, L-30 2nd A Main Road, HSR Layout, Bengaluru, Karnataka 560102

I researched google i got different solution so i got confused.


Solution

  • Update

    I have done a few modification to iVarun's solution. This is simpler. and working.

    First, add this function:

    func geocode(latitude: Double, longitude: Double, completion: @escaping (CLPlacemark?, Error?) -> ())  {
        CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: latitude, longitude: longitude)) { completion($0?.first, $1) }
    }
    

    After that, get the address:

    geocode(latitude: latvalue, longitude: lngvalue) { placemark, error in
        guard let placemark = placemark, error == nil else { return }
        // you should always update your UI in the main thread
        DispatchQueue.main.async {
            //  update UI here
            print("address1:", placemark.thoroughfare ?? "")
            print("address2:", placemark.subThoroughfare ?? "")
            print("city:",     placemark.locality ?? "")
            print("state:",    placemark.administrativeArea ?? "")
            print("zip code:", placemark.postalCode ?? "")
            print("country:",  placemark.country ?? "")
        }
    }
    

    Result:

    address1: Rua Casuarina
    address2: 443
    city: Rio de Janeiro
    state: RJ
    zip code: 20975
    country: Brazil
    

    As @iOSer indicated, CLPlacemark is capable of giving you this part of the string, However.

    You could split the string:

    let output:String = "[L-30 2nd A Main Road, L-30 2nd A Main Road, HSR Layout, Bengaluru, Karnataka 560102, India @ <+12.91597974,+77.62879254> +/- 100.00m, region CLCircularRegion (identifier:'<+12.91597974,+77.62879254> radius 70.94', center:<+12.91597974,+77.62879254>, radius:70.94m)]"
    let items = output.components(separatedBy: "@")
    print(items[0])
    

    Becuse the @ will be always included, you could skip the rest.

    Result: R