最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Trigger Google Places Autocomplete - Stack Overflow

programmeradmin3浏览0评论

I am trying to attach a virtual keyboard to bind to a Google places autoplete input.

If I physically type in a letter in the input, a Google-powered dropdown displays the autoplete predictions based on where the map is centered. If I click on a virtual keyboard key, the input gets updated properly, but the autoplete predictions are not updated.

I have tried to use the virtual keyboard change callback to trigger a "keyup" (stackoverflow results), "keypress", "keydown", "change" and I even tried this (demo):

change : function(e, keyboard, el) {
    google.maps.event.trigger(autoplete, 'place_changed');
}

which results in a javascript error basically showing that autoplete.getPlace(); does not return a result.

So now I've tried the code below (in this demo), which uses the change callback to trigger an autoplete prediction, but as you can see in that demo, the results differ.

change : function(e, keyboard, el) {
  var $el = $(el),
      service = new google.maps.places.AutopleteService();
  if ($el.val() !== "") {
    service.getQueryPredictions({ input: $(el).val() }, function(predictions, status) {
      if (status != google.maps.places.PlacesServiceStatus.OK) {
        alert(status);
        return;
      }
      var list = '', $results = $('#results');
      for (var i = 0, prediction; prediction = predictions[i]; i++) {
        list += '<li>' + prediction.description + '</li>';
      }
      $results.html(list);
    });
  }
}

Ideally, I want to trigger the already established autoplete prediction dropdown to update while typing on the virtual keyboard.

I am trying to attach a virtual keyboard to bind to a Google places autoplete input.

If I physically type in a letter in the input, a Google-powered dropdown displays the autoplete predictions based on where the map is centered. If I click on a virtual keyboard key, the input gets updated properly, but the autoplete predictions are not updated.

I have tried to use the virtual keyboard change callback to trigger a "keyup" (stackoverflow results), "keypress", "keydown", "change" and I even tried this (demo):

change : function(e, keyboard, el) {
    google.maps.event.trigger(autoplete, 'place_changed');
}

which results in a javascript error basically showing that autoplete.getPlace(); does not return a result.

So now I've tried the code below (in this demo), which uses the change callback to trigger an autoplete prediction, but as you can see in that demo, the results differ.

change : function(e, keyboard, el) {
  var $el = $(el),
      service = new google.maps.places.AutopleteService();
  if ($el.val() !== "") {
    service.getQueryPredictions({ input: $(el).val() }, function(predictions, status) {
      if (status != google.maps.places.PlacesServiceStatus.OK) {
        alert(status);
        return;
      }
      var list = '', $results = $('#results');
      for (var i = 0, prediction; prediction = predictions[i]; i++) {
        list += '<li>' + prediction.description + '</li>';
      }
      $results.html(list);
    });
  }
}

Ideally, I want to trigger the already established autoplete prediction dropdown to update while typing on the virtual keyboard.

Share Improve this question asked Feb 26, 2014 at 1:46 MottieMottie 86.4k30 gold badges130 silver badges248 bronze badges 1
  • Does this answer your question? How to avoid need for end-user to manually start the Google Places Autoplete dropdown? – Alex R Commented Oct 11, 2020 at 22:43
Add a ment  | 

1 Answer 1

Reset to default 11

I found my answer (demo)!

change : function(e, keyboard, el) {
    google.maps.event.trigger( el, 'focus', {} );
}
发布评论

评论列表(0)

  1. 暂无评论