【问题标题】:Getting "Uncaught ReferenceError: google is not defined" error using Google Maps API使用 Google Maps API 获取“未捕获的 ReferenceError:未定义 google”错误
【发布时间】:2016-11-03 13:50:16
【问题描述】:

我正在尝试自定义地图上的标记,但我不断收到“未定义谷歌”。我尝试了不同的方法,但仍然无法正常工作。在初始化地图之前,我使用了 API 密钥并包含了地图脚本。 这是我的html:

 <html class="no-js" lang="en-US" >
        <head>
            <meta charset="UTF-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
            <title>Home &#8211; Algebra</title>
    <link rel='dns-prefetch' href='//ajax.googleapis.com' />
    <link rel='dns-prefetch' href='//s.w.org' />
    <script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js?ver=2.1.0'></script>
    <link rel='https://api.w.org/' href='http://localhost/algebra/wp-json/' />
    <link rel="alternate" type="application/json+oembed" href="http://localhost/algebra/wp-json/oembed/1.0/embed?url=http%3A%2F%2Flocalhost%2Falgebra%2Fhome%2F" />
    <link rel="alternate" type="text/xml+oembed" href="http://localhost/algebra/wp-json/oembed/1.0/embed?url=http%3A%2F%2Flocalhost%2Falgebra%2Fhome%2F&#038;format=xml" />
        </head>
        <body class="page page-id-4 page-template page-template-page-templates page-template-page-home page-template-page-templatespage-home-php logged-in offcanvas">



    <header id="masthead" class="site-header" role="banner">
        <div class="top-strip"></div>
        <div class="top-nav"></div>
    </header>

    <section class="container">
<section class="map">
    <div id="map-container"></div>
  <div class="contact-container">
    <div class="contact">
      <img src="" alt="mail-icon">
      <span>You can contact us at info@algebraventures.com</span>
    </div>
  </div>
</section>
        </section>
        <div id="footer-container">
            <footer id="footer">
            </footer>
        </div>
  <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAKX-BAa2bSAiC89C38o8ir29Q7iOWdQ94&callback=initMap"
  type="text/javascript"></script>
<script type='text/javascript''>
var myCenter = new google.maps.LatLng(29.714954,32.368546);
    function initMap() {
  var mapProp = {
    center:myCenter,
    zoom:13,
    mapTypeId:google.maps.MapTypeId.ROADMAP
  };
  var map=new google.maps.Map(document.getElementById("map-container"),mapProp);
  var marker=new google.maps.Marker({
  position:myCenter,
  title: "AZHA"
  });
  marker.setMap(map);
  var infowindow = new google.maps.InfoWindow({
  content:"AZHA"
  });
  infowindow.open(map,marker);
}
// google.maps.event.addDomListener(window, 'load', initMap);
</script>
<script type='text/javascript' src='http://localhost/algebra/wp-includes/js/wp-embed.min.js?ver=4.6.1'></script>
</body>
</html>

非常感谢您的帮助,谢谢。

【问题讨论】:

    标签: javascript html google-maps


    【解决方案1】:

    如果您异步加载 API(使用asyncdefer&amp;callback=initMap),则需要将依赖于 API 的所有代码放入回调函数中(或在至少在加载 API 之前它不会执行的地方)。现在你的 myCenter 变量是在回调函数之外定义的。

    变化:

    <script type='text/javascript''>
    var myCenter = new google.maps.LatLng(29.714954,32.368546);
    function initMap() {
    

    收件人:

    <script type='text/javascript''>
    function initMap() {
      var myCenter = new google.maps.LatLng(29.714954,32.368546);
    

    proof of concept fiddle

    代码 sn-p:

    html,
    body,
    #map-container,
    .map,
    .container {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    <header id="masthead" class="site-header" role="banner">
      <div class="top-strip"></div>
      <div class="top-nav"></div>
    </header>
    
    <section class="container">
      <section class="map">
        <div id="map-container"></div>
        <div class="contact-container">
          <div class="contact">
            <img src="" alt="mail-icon">
            <span>You can contact us at info@algebraventures.com</span>
          </div>
        </div>
      </section>
    </section>
    <div id="footer-container">
      <footer id="footer">
      </footer>
    </div>
    <script type='text/javascript'>
      function initMap() {
          var myCenter = new google.maps.LatLng(29.714954, 32.368546);
          var mapProp = {
            center: myCenter,
            zoom: 13,
            mapTypeId: google.maps.MapTypeId.ROADMAP
          };
          var map = new google.maps.Map(document.getElementById("map-container"), mapProp);
          var marker = new google.maps.Marker({
            position: myCenter,
            title: "AZHA"
          });
          marker.setMap(map);
          var infowindow = new google.maps.InfoWindow({
            content: "AZHA"
          });
          infowindow.open(map, marker);
        }
        // google.maps.event.addDomListener(window, 'load', initMap);
    </script>
    <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk" type="text/javascript"></script>

    【讨论】:

      猜你喜欢
      • 2015-12-18
      • 2020-10-04
      • 1970-01-01
      • 2012-09-12
      • 1970-01-01
      • 2019-03-22
      • 2019-03-31
      • 1970-01-01
      • 2013-03-08
      相关资源
      最近更新 更多