-1

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

vyshnavi
  • 1
  • 2

1 Answers1

0

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")
             }
         }
     }
 }
 
  • with your code also iam facing issue and are you sure you have peru in your iPhone please check general-> region in iPhone settings @workingdog support Ukraine – vyshnavi Feb 16 '23 at 08:40
  • yes, setting region is on `Peru`, and all is working well for me. – workingdog support Ukraine Feb 16 '23 at 13:27
  • I copy pasted your code in a new project and set my device region to Peru and it does not work may be are you doing something with build settings? – vyshnavi Feb 17 '23 at 05:59
  • I have now Xcode 14.3 beta, and target ios 16.3. Created a new project with the code and no other setting changes. All works well, however I noticed that I get some warning in the Xcode console: `{"msg":"#NullIsland Received a latitude or longitude from getLocationForBundleID that was exactly zero"...`, the code still works. The placemark I get is: `[East Angamos Avenue, East Angamos Avenue, Lima, Peru @ <-12.11364117,-77.02725920> +/- 100.00m, region CLCircularRegion (identifier:'<-12.11364117,-77.02725920> radius 70.50', center:<-12.11364117,-77.02725920>, radius:70.50m)]` – workingdog support Ukraine Feb 17 '23 at 07:41
  • Note, I also tested on MacOS 13.2.1 only and also on MacCatalyst. Changing the region to `Peru` on my iMac. All works well, and I do not get the warning that I mentioned, seems to be something not related. I don't know what the problem could be. Note that I change the region but I am physically in Japan not Peru. – workingdog support Ukraine Feb 17 '23 at 08:00
  • can you pls share GitHub link of your project if possible – vyshnavi Feb 20 '23 at 06:08
  • here is the link to my test code on github: https://github.com/workingDog/GeocoderTest Where are you physically? As I understand it, Apple select some server based on your request location, and some do not behave well. – workingdog support Ukraine Feb 20 '23 at 08:38