Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google map autocomplete select first entry in list by default

I am using a gmap autocomplete and sometimes the user doesn't hit any choice in the suggestion list. For example he types "Paris" in the input field and believes the search will be performed with Paris, however has the 'place_changed' of the gmap autcomplete was never called, the search cannot be perfomed. How can I select by default the first choice of the suggestion list when the user doesn't make any choice ? I believe I could adapt the solution provided for the "enter issue" described here (Google maps Places API V3 autocomplete - select first option on enter) with a blur event handling, however this doesn't work.

Any hint ?

like image 214
queto putito Avatar asked Oct 05 '22 13:10

queto putito


1 Answers

I think this is kind of defeating the purpose of the auto complete.

You could just retrieve the autocomplete predictions pro grammatically like so:

function initialize() 
{
    var service = new google.maps.places.AutocompleteService();
    service.getQueryPredictions({ input: 'some address' }, callback);
}

function callback(predictions, status) 
{
   if (status != google.maps.places.PlacesServiceStatus.OK) 
   {
      alert(status);
      return;
   }  

   // Take the first result
   var result = predictions[0]

   // do as you wish with the result    
}

Hope that helps

like image 68
TResponse Avatar answered Oct 10 '22 02:10

TResponse