CLGeocoder region specific Error Domain=kCLErrorDomain Code=8 "(null)"

240 views Asked by At

I have a query regarding clgeocoder apis , basically in our app we have a provision to add address manually by user by giving region, city and place details etc; when I try to fetch lat long values via CLgeocoder geocodeAddressString api its throwing following error Error Domain=kCLErrorDomain Code=8 "(null)" (Here the region selected is peru). When i change region to India in device and add address manually it works and iam able to fetch lat long values.

Iam attaching code below for reference

Can some one help to resolve this region specific issue func getLatLong () { let address = " 1850, Av. angamos este , Municipalidad Metropolitana de Lima, Surquillo, PERU" let geocoder = CLGeocoder()

    geocoder.geocodeAddressString(address) { (placemarks, error) in
        if error == nil {
            print(placemarks)
        } else {
            print(error)
        }
        
    }
}

ima expecting to receive lat long values irrespective of selected region in device

1

There are 1 answers

7
workingdog support Ukraine On

Here is my test code, create a new XCode project (ios) and use this example code.

All works well for me, on MacOS 13.2, Xcode 14.2, tested on real ios 16.3 devices (not Previews), and macCatalyst.

 import Foundation
 import SwiftUI
 import CoreLocation
 
 struct ContentView: View {
     @State var txt = ""
     
     var body: some View {
         Text(txt)
             .onAppear {
                 getLatLong()
             }
     }
     
     func getLatLong() {
         let address = " 1850, Av. angamos este , Municipalidad Metropolitana de Lima, Surquillo, PERU"
         let geocoder = CLGeocoder()
         geocoder.geocodeAddressString(address) { (placemarks, error) in
             if error == nil {
                 print("\n---> placemarks: \(placemarks)\n")
                 if let first = placemarks?.first,
                    let coord = first.location?.coordinate {
                     txt = "lat: \(coord.latitude)  lon: \(coord.longitude)"
                 }
             } else {
                 print("\n---> error: \(error)\n")
             }
         }
     }
 }