Google地图会放置自动填充加载事件

Aks*_*hat 8 javascript google-maps autocomplete google-places-api

我正在使用Google Maps Places Autocomplete api.

在显示自动完成下拉列表之前,我想在搜索期间显示Ajax微调器.

如何确定地点结果何时准备好?是否有一些事件被解雇了?

这对于糟糕的互联网连接特别有用,因为延迟有时可能长达5秒.在疯狂按Enter键进行搜索之前,用户需要知道搜索框是自动完成输入框.

aga*_*din 3

您可以使用自动完成服务来获取查询预测。它有一个回调函数。

在您提供的链接的示例中,您可以将输入声明为脚本中的全局变量。并在初始化函数之后使用自动完成服务访问它:

var input;
function initialize () {
  ...
  input = document.getElementById('pac-input');
  ...
}

function showSpinner() {
  document.getElementById('spinner').style.display = 'block';

  var service = new google.maps.places.AutocompleteService();
  service.getQueryPredictions({ input: input }, callback);
}

function callback(predictions, status) {
  if (status != google.maps.places.PlacesServiceStatus.OK) {
    document.getElementById('spinner').style.display = 'none';
    return;
  }
}
Run Code Online (Sandbox Code Playgroud)

至于html文档的修改:

  <body>
    <input id="pac-input" class="controls" type="text"
        placeholder="Enter a location" onkeypress="showSpinner()">
    <img id="spinner" src="spinner.gif" style="position:absolute; top:0; right:0; width: 250px; display: none">
  </body>
Run Code Online (Sandbox Code Playgroud)

仅当一秒超时(即,如果用户的互联网连接速度很慢)时,我才会运行它,否则旋转器看起来就像一个潜意识图像。