4

I'm using Google Maps Places V3 autocomplete. I would like to have a functionality where if a user starts typing into the searchfield, the first item from the autocomplete drop down is automatically selected.

Similar to the search function in Facebook.

I already updated the Google maps autocomplete with the help of these 2 threads:

But I cannot find a solution for this new issue....

I posted my code here: http://jsfiddle.net/Chazz09/YfPv3/5/

$(document).ready(function(){
  var pac_input = document.getElementById('searchfield');
  // prevents enter key to submit form//    
  $('#searchfield').keydown(function (e) {
    if (e.which == 13 && $('.pac-container:visible').length) return false;
  });   
  // prevents enter key to submit form//

  (function pacSelectFirst(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.pac-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]);
        };
      }

      // add the modified listener
      _addEventListener.apply(input, [type, listener]);
    }

    if (input.addEventListener)
      input.addEventListener = addEventListenerWrapper;
    else if (input.attachEvent)
      input.attachEvent = addEventListenerWrapper;

  })(pac_input);

  $(document).ready(function() 
                    {
    function initialize() {
      var options = {
        types: ['geocode'],
        componentRestrictions: {country: "fr"}
      };
      var autocomplete = new google.maps.places.Autocomplete(pac_input, options);
    }
    google.maps.event.addDomListener(window, 'load', initialize);
  });
});
Community
  • 1
  • 1
Chazz
  • 305
  • 3
  • 12

2 Answers2

1

How about this?

$("input").keypress(function(event){
            if(event.keyCode == 13 || event.keyCode == 9) {
                $(event.target).blur();
                if($(".pac-container .pac-item:first span:eq(3)").text() == "")
                    firstValue = $(".pac-container .pac-item:first .pac-item-query").text();
                else
                    firstValue = $(".pac-container .pac-item:first .pac-item-query").text() + ", " + $(".pac-container .pac-item:first span:eq(3)").text();
                event.target.value = firstValue;

            } else
                return true;
});
Gangadhar Jannu
  • 4,136
  • 6
  • 29
  • 49
0

try this code...when your list opens..

$('#yourAddressTextBox').val($('.pac-container').find('.pac-item').eq(0).text());

your keydown function:

$('#searchfield').keydown(function (e) {
setTimeout(function(){
    $('#searchfield').val($('.pac-container').find('.pac-item').eq(0).text());},1000);
    if (e.which == 13 && $('.pac-container:visible').length) return false;
});
Premshankar Tiwari
  • 3,006
  • 3
  • 24
  • 30
  • Thanks for your suggestion, but I can't get it to work properly. Maybe I'm doing something wrong. Did you test your code? – Chazz Jul 23 '13 at 08:42
  • please put your code with my snippet – Premshankar Tiwari Jul 23 '13 at 09:40
  • I tried putting your snippet in different places, So no I addded a function when the list (.pac-containter) is visible: if (!$('.pac-container').is(':visible')) { $('#searchfield').val($('.pac-container').find('.pac- item').eq(0).text()); } But still no success, my code is probably incorrent. Posted the code here: http://jsfiddle.net/Chazz09/YfPv3/15/ – Chazz Jul 25 '13 at 08:32
  • I have edited my answer...you have to put this code in keydown(or keypress) function... – Premshankar Tiwari Jul 26 '13 at 18:39
  • Thanks for you answer. Your code sort of anwsers my question. The only problem is that users now are forced to pick the first suggestion from te dropdown list and it's almost impossible to change their input. Next to that if users click away from the search box their original input is still their. http://jsfiddle.net/YfPv3/19/ – Chazz Jul 30 '13 at 20:41
  • 1
    But thanks a lot for your help, your code does give me a good starting point – Chazz Jul 30 '13 at 20:41