AutoComplete Google Javascript



嗨,我在让自动完成显示时遇到问题,就像这里一样。我的代码适用于提交并正确显示地图,但不显示自动完成的结果。我想知道可能是什么问题。

这是我的网页

<div id="floating-panel">
  //this is the text field that does not display the autocomplete
  <input id="address" placeholder="Enter Starting Location" type="text"/>
  <input id="submit" type="button" value="Directions">
</div>
<div id="right-panel"></div>
<div id="map"></div>

这是我的Javascript:

 var autocomplete = new google.maps.places.Autocomplete(document.getElementById('address'));

这是我对自动完成的调用,API 已启用,我看到正在对数据进行调用,但没有返回结果:

 https://maps.googleapis.com/maps/api/js?key=xxx&libraries=places&callback=initMap: {type: external, attributes: {async: true}}

谷歌地图 API 在其新版本中有新的安全更改,使用 3.0 版,这对我有用:

<script src="https://maps.googleapis.com/maps/api/js?v=3.0&key=YOUR_KEY&libraries=places" async></script>

我用非常相似的代码得到了同样的事情。对我来说,该代码已经工作了几个月,就在今天,我收到了一位用户的报告,指出位置下拉列表不再显示。

var input = $("input[name='location']");
var formattedAddress = input.next("input[name='formattedLocation']");
var searchBox = new google.maps.places.SearchBox(input[0]);
searchBox.addListener('places_changed', function() {
   var places = searchBox.getPlaces();
   if (places.length == 0) {
      formattedAddress.val('');
      return;
   }
   formattedAddress.val(places[0].formatted_address);
 });

我在想也许我们超过了我们的每日配额,但从 Google API 控制台来看似乎并非如此。我在浏览器控制台中没有看到任何错误。如果我查看开发人员工具网络选项卡,我可以看到请求进入 https://maps.googleapis.com/maps/api/place/js/AutocompletionService.GetQueryPredictions,但响应似乎相当神秘(例如:/**/xdc._490z0c&& xdc._490z0c( [4] ( (,所以我无法判断好数据是否回来。

我想知道谷歌方面最近是否有一些我没有听说过的变化。

我的临时解决方案:我选择了体验式版本,https://maps.googleapis.com/maps/api/js?v=3.32并注意到在我的HTML底部有一个标签<div class="pac-container pac-logo hdpi">一旦你开始输入它就会更新,设置这个样式并得到修复。我发送了一个问题请求,我希望他们能为稳定版本解决此问题。

https://developers.google.com/maps/documentation/javascript/versions

如果您使用的是标准计划,请指定要使用发布版本而不是实验版本。

相关内容

最新更新