Reverse Geocoding

Hello,


I would like to know why some request with CLGeocoder().reverseGeocodeLocation succeeded, but do not give us the right information and enough information as far as the address, zipcode...are concerned ?


Exemple :

Search zipCode of Pointe-à-Pitre in Guadeloupe


1-

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Point-%C3%A0-pitre&key=AIzaSyCStBouFLrpQhqD4XAJYGsRhwEOsYt2jRM&language=fr


give us the "place_id": "ChIJBRWixeNFE4wRY2Ax28N7gRE"


2 - with the given place_id get details

https://maps.googleapis.com/maps/api/place/details/json?key=AIzaSyCStBouFLrpQhqD4XAJYGsRhwEOsYt2jRM&placeid=ChIJBRWixeNFE4wRY2Ax28N7gRE


give us latitude and longitude


let loc: CLLocation = CLLocation(latitude: -16.237687, longitude: -61.534042)


3 -


let loc: CLLocation = CLLocation(latitude: -16.237687, longitude: -61.534042)


CLGeocoder().reverseGeocodeLocation(loc, completionHandler: {(placemarks, error)->Void in

if (error != nil) {

print("Reverse geocoder failed with error" + (error?.localizedDescription)!)

return

}

if (placemarks?.count)! > 0 {

let pm = placemarks?[0]

self.displayLocationInfo(pm)

} else {

print("Problem with the data received from geocoder")

}

})


Error is nil, we got one placemark but :


First the (pm as! CLPlacemark).addressDictionary["Country"] = "Bolivie" wich is wrong


Then we do not have any information about

(pm as! CLPlacemark).locality, (pm as! CLPlacemark).postalCode wich are nil


Is there restriction or limit with using CLGeocoder().reverseGeocodeLocation ? Do we have any other solution or free APIs in this case ?


Thank you very much for any response.

These coordinates (negative latitude) is for a place in the east of Bolivie.


Pointe a Pitre is


latitude: +16.237687, longitude: -61.534042

Hi Claude,


Indeed it's latitude: +16.237687, longitude: -61.534042 : but the problem is the same, (pm as! CLPlacemark).locality, (pm as! CLPlacemark).postalCode are nil. But we got a valid (pm as! CLPlacemark).addressDictionary.

OK, so that solves the first error

First the (pm as! CLPlacemark).addressDictionary["Country"] = "Bolivie" wich is wrong



What do you get when you print(pm as! CLPlacemark) ?


Plesae post the result.


Do you run in simulator of on device ?


I Tried the following (sorry, I did not edit completely the code).


And it works


     func findPhysicalLocation(_ aLocation: CLLocation, placemark: CLPlacemark) {
          var myPresentLongitude : Double = 0.0
          var myPresentLatitude : Double = 0.0
        
          if let street = placemark.thoroughfare {
               physicalAddress = street
          } else {
               physicalAddress = ""
          }
          if let postalCode = placemark.postalCode { // 24.9.2017
               physicalAddress = physicalAddress + ", " + postalCode
          }
          if let localite = placemark.locality {
               let country = placemark.country
               // use them as needed
               myPresentLongitude = aLocation.coordinate.longitude
               myPresentLatitude = aLocation.coordinate.latitude
               print("latitude is", myPresentLatitude)
               print("longitude is", myPresentLongitude)
          }
     }

   
     func getPlacemarkFromLocation(_ location: CLLocation) {
          print(location)
          CLGeocoder().reverseGeocodeLocation(location, completionHandler:
               {(placemarks, error) in
                    if (error != nil) {
                         print("reverse geodcode fail: \(error!.localizedDescription)")
                    }
                    if let _ = error  {
                    } else {
                        if let pm = placemarks {
                         print("for reverse", location)
                         print("place", pm)
                              if pm.count > 0 {
                                   self.findPhysicalLocation(location, placemark:placemarks![0])
                              }
                         }
                    }
          })
     }
   
     @IBAction func messageLocationButton(_ sender: UIButton) {     // a button to test
          let loc: CLLocation = CLLocation(latitude: 16.237687, longitude: -61.534042)
          getPlacemarkFromLocation(loc)    // This will update physicalAddress
          print(physicalAddress)

     }


I get :

for reverse <+16.23768700,-61.53404200> +/- 0.00m (speed -1.00 mps / course -1.00) @ 1/2/19, 11:25:23 PM Central European Standard Time

place [Place de la Victoire, Place de la Victoire, Quai Perrinon, Pointe-A-Pitre, Guadeloupe @ <+16.23768700,-61.53404200> +/- 100.00m, region CLCircularRegion (identifier:'<+16.23768700,-61.53404200> radius 141.86', center:<+16.23768700,-61.53404200>, radius:141.86m)]

latitude is 16.237687

longitude is -61.534042


Yes that exactly what i got : but the postalCode is nil, and this was the information that was interesting for me. And this is apparently the case for all DOM-TOM...don't know why ?

I have printed more details.


     func findPhysicalLocation(_ aLocation: CLLocation, placemark: CLPlacemark) {
          var myPresentLongitude : Double = 0.0
          var myPresentLatitude : Double = 0.0
         
          if let street = placemark.thoroughfare {
               physicalAddress = "street " + street
          } else {
               physicalAddress = "no street"
          }
          if let locality = placemark.locality { // 24.9.2017
               physicalAddress = physicalAddress + ", " + "locality " + locality
          } else {
               physicalAddress = physicalAddress + ", no locality"
          }
          if let subLocality = placemark.subLocality { // 24.9.2017
               physicalAddress = physicalAddress + ", " + "subLocality " + subLocality
          } else {
               physicalAddress = physicalAddress + ", no subLocality"
          }
         if let postalCode = placemark.postalCode { // 24.9.2017
               physicalAddress = physicalAddress + ", " + "postalCode " + postalCode
          } else {
               physicalAddress = physicalAddress + ", no postalCode"
          }
          if let country = placemark.country {
               physicalAddress = physicalAddress + ", " + "country " + country
          } else {
               physicalAddress = physicalAddress + ", no country"
          }
     }



And get this


physicalAddress street Quai Perrinon, locality Pointe-A-Pitre, subLocality Pointe-à-Pitre, no postalCode, country Guadeloupe

compared with

physicalAddress street Infinite Loop, locality Cupertino, no subLocality, postalCode 95014, country United States


So, there is a locality, but no postal code.

You should file a bug report.

Reverse Geocoding
 
 
Q