admin.php:26未捕获的ReferenceError:未定义谷歌

时间:2021-07-15 19:17:00

everytime i make var directionsService = new google.maps.DirectionsService(); in console i got admin.php:26 Uncaught ReferenceError: google is not defined. can somebody help me fix this?

每次我做var directionsService = new google.maps.DirectionsService();在控制台我得到了admin.php:26未捕获的ReferenceError:谷歌没有定义。有人可以帮我解决这个问题吗?

<script>
  var directionsService = new google.maps.DirectionsService();
  var directionsDisplay;

  function loadScript(){
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://maps.googleapis.com/maps/api/js?key=AIzaSyA6FWEcLI-sIDj2ViI02kjL3KV-njatBro&sensor=false&callback=initMap";
    document.body.appendChild(script);
  }
  window.onload = loadScript;
  ....
    </script>

1 个解决方案

#1


0  

You're calling the maps app before you load it and then not providing the callback function. Try this:

您在加载地图应用程序之前调用地图应用程序,然后不提供回调功能。试试这个:

<script>
  function loadScript(){
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://maps.googleapis.com/maps/api/js?key=AIzaSyA6FWEcLI-sIDj2ViI02kjL3KV-njatBro&sensor=false&callback=initMap";
    document.body.appendChild(script);

  }
  window.onload = loadScript;

  function initMap() {
    var directionsService = new google.maps.DirectionsService(document.getElementById('map'));
    var directionsDisplay;
    console.log( 'Script loaded.' );
  }
</script>

#1


0  

You're calling the maps app before you load it and then not providing the callback function. Try this:

您在加载地图应用程序之前调用地图应用程序,然后不提供回调功能。试试这个:

<script>
  function loadScript(){
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://maps.googleapis.com/maps/api/js?key=AIzaSyA6FWEcLI-sIDj2ViI02kjL3KV-njatBro&sensor=false&callback=initMap";
    document.body.appendChild(script);

  }
  window.onload = loadScript;

  function initMap() {
    var directionsService = new google.maps.DirectionsService(document.getElementById('map'));
    var directionsDisplay;
    console.log( 'Script loaded.' );
  }
</script>