Google Places Autocomplete - Pick first result on Enter key?

51,118

Solution 1

I've read the many answers of this question, and of the linked questions' answers so many times, before finding that the best answer is this one (Nota: sadly, it's not the accepted answer!).

I've modified 2 or 3 lines to turn it into a ready-to-use function that you can copy/paste in your code and apply to many input elements if needed. Here it is:

var selectFirstOnEnter = function(input) {  // store the original event binding function
    var _addEventListener = (input.addEventListener) ? input.addEventListener : input.attachEvent;
    function addEventListenerWrapper(type, listener) {  // Simulate a 'down arrow' keypress on hitting 'return' when no pac suggestion is selected, and then trigger the original listener.
        if (type == "keydown") { 
            var orig_listener = listener;
            listener = function(event) {
                var suggestion_selected = $(".pac-item-selected").length > 0;
                if (event.which == 13 && !suggestion_selected) { 
                    var simulated_downarrow = $.Event("keydown", {keyCode: 40, which: 40}); 
                    orig_listener.apply(input, [simulated_downarrow]); 
                }
                orig_listener.apply(input, [event]);
            };
        }
        _addEventListener.apply(input, [type, listener]); // add the modified listener
    }
    if (input.addEventListener) { 
        input.addEventListener = addEventListenerWrapper; 
    } else if (input.attachEvent) { 
        input.attachEvent = addEventListenerWrapper; 
    }
}

Usage:

selectFirstOnEnter(input1);
selectFirstOnEnter(input2);
...

Solution 2

I am reposting my answer from Google maps Places API V3 autocomplete - select first option on enter:

It seems there is a much better and clean solution: To use google.maps.places.SearchBox instead of google.maps.places.Autocomplete. A code is almost the same, just getting the first from multiple places. On pressing the Enter the the correct list is returned - so it runs out of the box and there is no need for hacks.

See the example HTML page:

http://rawgithub.com/klokan/8408394/raw/5ab795fb36c67ad73c215269f61c7648633ae53e/places-enter-first-item.html

The relevant code snippet is:

var searchBox = new google.maps.places.SearchBox(document.getElementById('searchinput'));

google.maps.event.addListener(searchBox, 'places_changed', function() {
  var place = searchBox.getPlaces()[0];

  if (!place.geometry) return;

  if (place.geometry.viewport) {
    map.fitBounds(place.geometry.viewport);
  } else {
    map.setCenter(place.geometry.location);
    map.setZoom(16);
  }
});

The complete source code of the example is at: https://gist.github.com/klokan/8408394

Solution 3

In my site to achieve this same functionality I neeeded the jQuery simulate plugin (https://github.com/jquery/jquery-simulate) and then attach the event:

$("input#autocomplete").focusin(function () {
    $(document).keypress(function (e) {
        if (e.which == 13) {
            $("input#autocomplete").trigger('focus');
            $("input#autocomplete").simulate('keydown', { keyCode: $.ui.keyCode.DOWN } ).simulate('keydown', { keyCode: $.ui.keyCode.ENTER });
        }
    });
});

The plugin will simulate the action of press the key DOWN and then ENTER, ENTER itself does not work and I couldn't find another way to select the first option.

Hope this helps

Solution 4

Working Solution that listens to if the user has started to navigate down the list with the keyboard rather than triggering the false navigation each time

https://codepen.io/callam/pen/RgzxZB

Here are the important bits

// search input
const searchInput = document.getElementById('js-search-input');

// Google Maps autocomplete
const autocomplete = new google.maps.places.Autocomplete(searchInput);

// Has user pressed the down key to navigate autocomplete options?
let hasDownBeenPressed = false;

// Listener outside to stop nested loop returning odd results
searchInput.addEventListener('keydown', (e) => {
    if (e.keyCode === 40) {
        hasDownBeenPressed = true;
    }
});

// GoogleMaps API custom eventlistener method
google.maps.event.addDomListener(searchInput, 'keydown', (e) => {

    // Maps API e.stopPropagation();
    e.cancelBubble = true;

    // If enter key, or tab key
    if (e.keyCode === 13 || e.keyCode === 9) {
        // If user isn't navigating using arrows and this hasn't ran yet
        if (!hasDownBeenPressed && !e.hasRanOnce) {
            google.maps.event.trigger(e.target, 'keydown', {
                keyCode: 40,
                hasRanOnce: true,
            });
        }
    }
});

 // Clear the input on focus, reset hasDownBeenPressed
searchInput.addEventListener('focus', () => {
    hasDownBeenPressed = false;
    searchInput.value = '';
});

// place_changed GoogleMaps listener when we do submit
google.maps.event.addListener(autocomplete, 'place_changed', function() {

    // Get the place info from the autocomplete Api
    const place = autocomplete.getPlace();

    //If we can find the place lets go to it
    if (typeof place.address_components !== 'undefined') {          
        // reset hasDownBeenPressed in case they don't unfocus
        hasDownBeenPressed = false;
    }

});

Solution 5

This is the easiest way that solved to me:

autocomplete.addListener('place_changed', function() {
  if(event.keyCode == 13 || event.keyCode == 9) { // detect the enter key
    var firstValue = $(".pac-container .pac-item:first").text(); // assign to this variable the first string from the autocomplete dropdown
     }
    $('#search-address').val(firstValue); // add this string to input
    console.log(firstValue); // display the string on your browser console to check what it is
   //(...) add the rest of your code here
  });
}
Share:
51,118
NChase
Author by

NChase

Web developer somewhere between total amateur and real professional.

Updated on May 30, 2020

Comments

  • NChase
    NChase almost 4 years

    I'm using a Google Places Autocomplete and I simply want it to select the top item in the results list when the enter key is pressed in the form field and suggestions exist. I know this has been asked before:

    Google maps Places API V3 autocomplete - select first option on enter

    Google maps Places API V3 autocomplete - select first option on enter (and have it stay that way)

    But the answers in those questions don't seem to actually work, or they address specific added functionality.

    It also seems like something like the following should work (but it doesn't):

    $("input#autocomplete").keydown(function(e) {
      if (e.which == 13) {          
        //if there are suggestions...
        if ($(".pac-container .pac-item").length) {
          //click on the first item in the list or simulate a down arrow key event
          //it does get this far, but I can't find a way to actually select the item
          $(".pac-container .pac-item:first").click();
        } else {
          //there are no suggestions
        }
      }
    });
    

    Any suggestions would be greatly appreciated!

  • metsfan
    metsfan almost 10 years
    SeachBox doesn't let you filter by (region) or (cities), or any type, for that matter.
  • pkExec
    pkExec over 9 years
    I tested this, and it doesn't work as expected. The SearchBox does an "autocomplete" request when you type, but when you press enter, it does a request at maps.googleapis.com/maps/api/js/PlaceService.QueryPlaces, using the string you entered. This may or may not return actual results. For example, by typing "naxo", the autocomplete list returns as a first result "Naxos ke Mikres Kyklades" (maybe this is based on locale?). Pressing enter, without selecting a list item, returns 0 results.
  • dnlmzw
    dnlmzw about 9 years
    This doesn't work in situations where you navigate with your arrow-keys and select it by pressing the enter key.
  • Adam Starrh
    Adam Starrh over 8 years
    Search box is so convenient. I really wish it would let me restrict results to geocode or addresses.
  • user151496
    user151496 about 8 years
    but search box is different from autocomplete. it's more for things like "coffee in new york"
  • user151496
    user151496 about 8 years
    @dnlmzw if you navigate with arrow keys and select it with enter then you get "place_change" fired...
  • jave.web
    jave.web almost 8 years
    @user151496 that's true, but it will not work if your field is a <form> field, then you should do e.preventDefault(); on enter (13/$.ui.keyCode.ENTER)keydown, if you don't, the request's response probably won't make it back in time :)
  • Kelderic
    Kelderic almost 7 years
    I've been using Autocomplete for about a year now, and after reading this and switching to SearchBox, everything is so much better. I had a special "getPlaceFromAutocompleteList" function as a work around, but this fixes several frustrating edge case bugs. Thank you!
  • theBugger
    theBugger about 6 years
    Doesn't work anymore. Is there a new way to do this?
  • Mladen
    Mladen almost 6 years
    This works like a charm. It requires jQuery. If you're on Angular like me, here's a little guide on how to install it: medium.com/@swarnakishore/…
  • Antony D'Andrea
    Antony D'Andrea almost 6 years
    This worked perfectly for me after hours of searching and trying myself to hack similar solutions.
  • woodii
    woodii over 5 years
    also be sure that you call the function before var autocomplete = new google.maps.places.Autocomplete(input); otherwise it won't work
  • Derenir
    Derenir about 5 years
    Good answer, terribly formatted code. Thanks anyway :)
  • Gael
    Gael over 4 years
    I strongly recommend you to choose the AutocompleteService instead of SearchBox widget. Especially if price matter for you. See this blog article to help you understand the differences: blog.woosmap.com/…
  • Basj
    Basj almost 4 years
    @Derenir fixed!
  • Techiemanu
    Techiemanu over 3 years
    I modified above fiunction and it work for me like a charm. // listner to make kepdown to true and selecet first search result this.searchElementRef.nativeElement.addEventListener('keydow‌​n', (e) => { if (e.keyCode === 13 || e.keyCode === 9) { var firstValue = $('.pac-container .pac-item:first').text(); // If user isn't navigating using arrows and this hasn't ran yet this.txtValue = firstValue; google.maps.event.trigger(e.target, 'keydown', { keyCode: 40, hasRanOnce: true, }); } });
  • RobbTe
    RobbTe over 2 years
    this seems to cause an error TypeError: a.stopPropagation is not a function error any thoughts on this?