google map autocomplete select first entry in list by default
Asked Answered
I

1

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 ?

Impractical answered 29/3, 2013 at 18:26 Comment(0)
S
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

Sinistrodextral answered 21/1, 2015 at 1:57 Comment(3)
You are the man!! It was blowing my head 2 days. :)) Thank you!Philanthropy
Hello , I am using angular google maps , can you tell me please how can I select the first prediction ?Grotius
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 luckSinistrodextral

© 2022 - 2024 — McMap. All rights reserved.