Esri / esri-leaflet-geocoder

helpers for using the ArcGIS World Geocoding Service in Leaflet
http://esri.github.io/esri-leaflet/examples/geocoding-control.html
Apache License 2.0
244 stars 101 forks source link

L.esri.Geocoding.reverseGeocode() no longer seems to work #287

Closed kjrhody closed 2 years ago

kjrhody commented 2 years ago

Describe the bug

Under the terms described in the ArcGIS Developer documentation, we had been using the L.esri.Geocoding.reverseGeocode() function from this library under the free requirements, and without the need to provide a token parameter. But recently we started not receiving a response, and receiving an error that says we need to provide a token. So, two questions:

  1. Is this method still supported by ESRI leaflet?
  2. Can we pass an empty token string if using it under free requirements?

Thanks for any help.

Reproduction

function esriGeocode(lat, lon) {
    let esriGeocode = L.esri.Geocoding.reverseGeocode()
        .latlng([lat, lon])
        .run(function(error, result, response) {
            let city = response.address.City;
            let state = response.address.Region;
            let zip = response.address.Postal;
            let county = response.address.Subregion;
         });
}

Logs

Printing out error returns

{code: 499, message: 'Token Required', details: Array(0)}
code: 499
details: []
message: "Token Required

Printing outresult and response returns undefined and null respectively, which results in our code failing to return anything:

site.js:292 Uncaught TypeError: Cannot read properties of null (reading 'address')
    at site.js:292:33
    at NewClass.<anonymous> (esri-leaflet-geocoder:148:18)
    at XMLHttpRequest.httpRequest.onreadystatechange (esri-leaflet:220:18)

System Info

https://unpkg.com/esri-leaflet@3.0.4/dist/esri-leaflet-debug.js
https://unpkg.com/esri-leaflet-geocoder@3.1.1/dist/esri-leaflet-geocoder-debug.js

Additional Information

No response

gavinr commented 2 years ago

Thank you for the question. At version 3.0.0, Esri Leaflet Geocoder switched the default geocoding url from https://geocode.arcgis.com/... to https://geocode-api.arcgis.com/....

The reverse geocode that Esri Leaflet Geocoder is doing looks like this:

As you can see if you click those two links, the first one succeeds but the latter returns the token error. I need to get some additional informtion on this before I can tell you what is the expected behavior, but a short-term workaround would be for you to switch to using the v2.* version of the library: https://unpkg.com/esri-leaflet-geocoder@2.3.4/dist/esri-leaflet-geocoder.js if you use that, it will use https://geocode.arcgis.com... and give you the behavior you had previously (see demo here)

kjrhody commented 2 years ago

@gavinr Thank you so much for your quick response! To test, I updated the reference in our code with the URL you suggested, and that does indeed fix the issue! Really appreciate your help.

gavinr commented 2 years ago

The documentation here was updated to explain token/apikey requirements for https://geocode-api.arcgis.com, the endpoint that esri-leaflet-geocoder uses by default:

https://developers.arcgis.com/documentation/mapping-apis-and-services/search/services/geocoding-service/