perliedman / leaflet-control-geocoder

A simple geocoder form to locate places. Easily extended to multiple data providers.
http://www.liedman.net/leaflet-control-geocoder/
BSD 2-Clause "Simplified" License
560 stars 220 forks source link

Google suggest in search box #292

Open yunai39 opened 3 years ago

yunai39 commented 3 years ago

Hello,

First of all thx for the plugin it work great.

I'm currently looking for how to implements autocomplete with google by using google api (google.maps.places.AutocompleteService and google.maps.places.PlacesService). I managed to do what I wanted but it is quite messy.

I created suggest function in my geocoder like this

  suggest: function(query, cb, context) {
    var self = this;
    this.autocomplete.getPlacePredictions({ input: query }, function (predictions, status) {
      cb.call(context, self._decodePrediction(predictions));
    });
  },

this.autocomplete is google api for autocomplete. Unfortunately for me, the autocomplete api for google does not send back all the information needed. There is no gps coordinates and no details about the address. Instead I get a place_id that I can use to fetch those information throught the google.maps.places.PlacesService.

I could do that in _decodePrediction but I would be making a lot of call to the google api that are not needed. Instead I wanted to make the call only when the user choose an adresse in the autocomplete.

Is there any way that inside my geocoder, I can create a function that will be called when the user select a row in the autocomplete ?

Hope that I managed to explain my problem.