0

I'm using Leaflet Control Geocoder to implement Google search, I am trying to pass my API key from Google Geocoder

        let googleGeoCoderProvider = L.Control.Geocoder.google('MYGOOGLEAPIKEY');

    L.Control.geocoder({
        geocoder: googleGeoCoderProvider,
    }).addTo(map);

but the results in the console are https://maps.googleapis.com/maps/api/geocode/json?key=undefined&address=trompillo

is processing API key as undefined, the same problem with L.Control.Geocoder.Google('MYGOOGLEAPIKEY');

for it to work, I had to open the plugin code and directly replace key: with my APIKEY

    var Google = /*#__PURE__*/function () {
  function Google(options) {
    this.options = {
      serviceUrl: 'https://maps.googleapis.com/maps/api/geocode/json'
    };
    L__namespace.Util.setOptions(this, options);
  }

  var _proto = Google.prototype;

  _proto.geocode = function geocode(query, cb, context) {
    var params = geocodingParams(this.options, {
      key: 'MYAPIKEY',
      address: query
    });

how can I pass the API key from my JS code?

2
  • Have you paid for the subscription? As far as I am concerned Google has a few API keys. Check you use the right one.
    – MKR
    Jan 6 at 13:53
  • @MKR it is correct, because if I put my API key directly in the plugin code, it works Jan 6 at 14:05
0

Solved:

    let googleGeoCoderProvider = L.Control.Geocoder.google({
        apiKey: 'MIGOOGLEGEOCODERAPIKEY',
    });

Not the answer you're looking for? Browse other questions tagged or ask your own question.