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

apikey errors with geocode and reverseGeocode #294

Closed TannerJuby1 closed 2 years ago

TannerJuby1 commented 2 years ago

I am trying to use this package with my Gatsby (react) project but am running into issues with using my apikey.

Due to an issue I recently opened I import this package directly into my components to try and use

import * as ELG from 'esri-leaflet-geocoder'

When I use geosearch, the api key is applied and it works as expected:

const searchControl = ELG.geosearch({
  providers: [
    ELG.arcgisOnlineProvider({
      apikey: process.env.ARCGIS_API_KEY,
    }),
  ],
}).addTo(map)

But the geocode and reverseGeocode functions do not accept the apikey I pass in and I just get an error response about token being required. I've tried the following methods to pass in the apikey:

// geosearch method
ELG.reverseGeocode({
  providers: [
    apikey: process.env.ARCGIS_API_KEY
  ]
})...

// documentation method
ELG.reverseGeocode({
  apiKey: process.env.ARCGIS_API_KEY
})...

// documentation method 2
ELG.reverseGeocode({
  apikey: process.env.ARCGIS_API_KEY
})...

and none of these work, I always get the Token Required error response.

What is the proper way to pass the token to these methods?

gavinr commented 2 years ago

Hi, thank you for logging the issue. I think you can pass the apikey with .geocode() - here is sample code:

import * as esriLeafletGeocoder from "esri-leaflet-geocoder";

esriLeafletGeocoder
  .geocode({
    apikey: "...."
  })
  .text("380 New York St, Redlands, CA")
  .run(function (err, results, response) {
    if (err) {
      console.log(err);
      return;
    }
    console.log("geocode results:", results);
  });

Here is a sample showing it working: https://codepen.io/gavinr/pen/mdpXXJd?editors=0010

For .reverseGeocode() trying to follow the same pattern, I'm getting an error. I think it's an issue that needs correction in https://github.com/Esri/esri-leaflet-geocoder/blob/master/src/Tasks/ReverseGeocode.js (similar to the change in #278)

gavinr commented 2 years ago

This is fixed in version 3.1.3.