Google Places API错误:Edge和Firefox未定义“ google”

时间:2019-01-31 17:45:45

标签: jquery api firefox google-api microsoft-edge

所以有两个输入,当您开始输入地址时,它将给您结果列表。它可以在chrome上运行,但是由于某些原因,我不知道为什么它会在Edge中产生此错误:

enter image description here

  

Edge版本:Microsoft Edge 42.17134.1.0

     

Firefox版本:65.0版(64位)

这里是jsfiddle

  <html lang="en">

    <head>
      <meta charset="UTF-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <meta http-equiv="X-UA-Compatible" content="ie=edge" />
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS"
        crossorigin="anonymous" />

      <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
        crossorigin="anonymous"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut"
        crossorigin="anonymous"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k"
        crossorigin="anonymous"></script>
    </head>

    <body>
      <form id="distance_form">
        <div class="form-group"><label>Origin: </label> <input class="form-control" id="from_places" placeholder="Enter a location" />
          <input id="origin" name="origin" required="" type="hidden" />
        </div>

        <div class="form-group"><label>Destination: </label> <input class="form-control" id="to_places" placeholder="Enter a location" />
          <input id="destination" name="destination" required="" type="hidden" />
        </div>

      </form>
      <script>
      $(function () {
          // add input listeners
          google.maps.event.addDomListener(window, "load", function () {
            var from_places = new google.maps.places.Autocomplete(
              document.getElementById("from_places")
            );
            var to_places = new google.maps.places.Autocomplete(
              document.getElementById("to_places")
            );
          });
        });
      </script>
      


      <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCteSKX5TJVTK5GAdDktqmE2ebuDSPTU-4&libraries=places&language=en"></script>
    </body>

    </html>

1 个答案:

答案 0 :(得分:1)

尝试如下修改您的代码:

Google Maps API准备就绪后,它将使用回调参数调用指定的函数(initMap函数)。

<script>
    function initMap() {
        // add input listeners
        var from_places = new google.maps.places.Autocomplete(
            document.getElementById("from_places")
        );
        var to_places = new google.maps.places.Autocomplete(
            document.getElementById("to_places")
        );
    }
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCteSKX5TJVTK5GAdDktqmE2ebuDSPTU-4&libraries=places&language=en&callback=initMap"></script>

输出如下(使用Edge浏览器42.17134.1.0版本):

enter image description here

有关使用Google Maps API的更多详细信息,您可以检查this tutorialGoogle Maps API Place Autocomplete