6

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 ?

Community
  • 1
  • 1
queto putito
  • 253
  • 3
  • 15

1 Answers1

4

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

TResponse
  • 3,940
  • 7
  • 43
  • 63
  • You are the man!! It was blowing my head 2 days. :)) Thank you! – zur4ik Jan 27 '15 at 07:13
  • Hello , I am using angular google maps , can you tell me please how can I select the first prediction ? – Hasan Kanaan Aug 13 '20 at 09:26
  • Hi @hasan I am sorry but I am not too familiar with Angular but am with React. I would imagine you should be able to do it by index somehow. Unfortunately I cannot be too much help with Angular. Good luck – TResponse Aug 13 '20 at 09:29