触发Google地方信息自动完成 [英] Trigger Google Places Autocomplete

查看:94
本文介绍了触发Google地方信息自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接虚拟键盘以绑定到Google地方信息自动完成输入.

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

如果我在输入中实际输入字母,则Google支持的下拉列表会根据地图的中心位置显示自动填充预测.如果我单击虚拟键盘键,则输入会正确更新,但自动完成预测不会更新.

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

我尝试使用虚拟键盘change回调来触发"keyup"(堆栈溢出结果),"keypress","keydown","change",我什至尝试了此操作(

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(autocomplete, 'place_changed');
}

这会导致javascript错误,基本上表明autocomplete.getPlace();没有返回结果.

which results in a javascript error basically showing that autocomplete.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 autocomplete 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.AutocompleteService();
  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 autocomplete prediction dropdown to update while typing on the virtual keyboard.

推荐答案

我找到了答案(演示)!

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

这篇关于触发Google地方信息自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆