【问题标题】:Autocomplete places search not working自动完成地点搜索不起作用
【发布时间】:2017-09-16 18:30:49
【问题描述】:
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDQ7XPD_fZf8YCv-1gEE_Yid3xzFaEy7DU&libraries=places" async defer></script>

<script type="text/javascript">
      var defaultBounds = new google.maps.LatLngBounds(
      new google.maps.LatLng(-33.8902, 151.1759),
      new google.maps.LatLng(-33.8474, 151.2631));

      var input = document.getElementById('searchbox');
      var options = {
       bounds: defaultBounds,
       types: ['establishment']
    };

     autocomplete = new google.maps.places.Autocomplete(input, options);
</script>

<div class="col-md-6">
<form>
  <div class="form-group">
    <label for="PickUp">Pick Up location</label>
    <input type="text" id="searchbox" class="form-control" placeholder="Search...">
  </div>

当我在文本框中输入内容时,什么也没有发生。这段代码有什么问题?上面的代码引用了google web developer site的代码。

【问题讨论】:

    标签: javascript api dictionary google-maps-api-3


    【解决方案1】:

    您在脚本 src url 中缺少 &amp;callback=init。并将所有脚本代码放入function init(){...}

    function init() {
      var defaultBounds = new google.maps.LatLngBounds(
        new google.maps.LatLng(-33.8902, 151.1759),
        new google.maps.LatLng(-33.8474, 151.2631));
    
      var input = document.getElementById('searchbox');
      var options = {
        bounds: defaultBounds,
        types: ['establishment']
      };
    
      autocomplete = new google.maps.places.Autocomplete(input, options);
    }
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDQ7XPD_fZf8YCv-1gEE_Yid3xzFaEy7DU&libraries=places&callback=init" async defer></script>
    
    <div class="col-md-6">
      <form>
        <div class="form-group">
          <label for="PickUp">Pick Up location</label>
          <input type="text" id="searchbox" class="form-control" placeholder="Search...">
        </div>
      </form>
    </div>

    【讨论】:

    • 感谢您的回答,我可以为我的网站谷歌地图和自动完成搜索使用相同的 API 密钥,还是需要第二个密钥? @深3015
    • 我对谷歌地图和自动完成使用相同的键,但其中任何一个都不起作用,可能是什么问题?
    • 检查jsfiddle.net/deep3015/7z7sbnt4,两者都使用一个键
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    相关资源
    最近更新 更多