javascript - Google map API v3 - 信息窗口未呈现的多个标记

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

总的来说,我对编码和 API 很陌生,并且使用另一个教程,我成功地获得了带有多个标记和信息窗口的 map 。

从本地源预览时, map 呈现完美,但上传到托管站点时显示空白。

有人能够对可能出现的问题提供任何见解吗?我在下面包含了我的代码。

<!DOCTYPE html>
<html> 
<head> 
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
  <meta content="text/html">
  <meta charset=UTF-8"> 

  <title>Google Maps Multiple Markers</title> 
  <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 400px;
    width: 600px;
        margin: 0;
        padding: 0;
      }
    </style>

</head> 
<body>
  <div id="map"></div>

  <script type="text/javascript">
    var locations = [
      ['<h3>Bay of Islands</h3>' +
        '<p>The London Eye is a giant Ferris wheel situated on the banks of the River Thames. The entire structure is 135 metres (443 ft) tall and the wheel has a diameter of 120 metres (394 ft).</p>', -35.165034, 174.162854, 5],
      ['<h3>Whangarei</h3>' +
        '<p>The London Eye is a giant Ferris wheel situated on the banks of the River Thames. The entire structure is 135 metres (443 ft) tall and the wheel has a diameter of 120 metres (394 ft).</p>', -35.696121, 174.300132, 4],
      ['<h3>Mangawhai Heads</h3>' +
        '<p>The London Eye is a giant Ferris wheel situated on the banks of the River Thames. The entire structure is 135 metres (443 ft) tall and the wheel has a diameter of 120 metres (394 ft).</p>', -36.113032, 174.559536, 3],
      ['<h3>Auckland Hussies</h3>' +
        '<p>The London Eye is a giant Ferris wheel situated on the banks of the River Thames. The entire structure is 135 metres (443 ft) tall and the wheel has a diameter of 120 metres (394 ft).</p>', -36.852924, 174.750234, 2],
      ['<h3>Auckland</h3>' +
        '<p>The London Eye is a giant Ferris wheel situated on the banks of the River Thames. The entire structure is 135 metres (443 ft) tall and the wheel has a diameter of 120 metres (394 ft).</p>', -36.868273, 174.711450, 1]
    ];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 8,
      center: new google.maps.LatLng(-40.9006, 174.8860),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;
    var markers = new Array();

    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });

      markers.push(marker);

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }

    function AutoCenter() {
      //  Create a new viewpoint bound
      var bounds = new google.maps.LatLngBounds();
      //  Go through each...
      $.each(markers, function (index, marker) {
      bounds.extend(marker.position);
      });
      //  Fit these bounds to the map
      map.fitBounds(bounds);
    }
    AutoCenter();

  </script> 

    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
    </script>

</body>
</html>

最佳答案

我在您的页面(本地)中遇到两个 JavaScript 错误:

  • Uncaught ReferenceError: google is not defined
  • "initMap is not a function"

第一个是因为您正在使用 async defercallback=initMap在脚本加载中(异步加载脚本)

第二个是因为你没有initMap功能。

如果您同步加载脚本(删除 async defer 和回调参数),就可以解决这两个问题(尽管您可能想调查为什么 Google 建议异步加载脚本,而不是在所有示例中都这样做)。

要进行此更改:

<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>

致:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY">
</script>

并将脚本包含移动到 <head>文档的内容。

一旦我这样做了,我就会收到另一个 javascript 错误:

Uncaught ReferenceError: $ is not defined

(因为您没有包含 JQuery,所以要解决此问题,请在文档的头部包含 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>)

link to working page

更新代码

<!DOCTYPE html>
<html> 
<head> 
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
  <meta content="text/html">
  <meta charset=UTF-8"> 

  <title>Google Maps Multiple Markers</title> 
  <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 400px;
    width: 600px;
        margin: 0;
        padding: 0;
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCxitB5jQcw7weQdg9MqBRfxr6mj81wT7I"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</head> 
<body>
  <div id="map"></div>

  <script type="text/javascript">
    var locations = [
      ['<h3>Bay of Islands</h3>' +
        '<p>The London Eye is a giant Ferris wheel situated on the banks of the River Thames. The entire structure is 135 metres (443 ft) tall and the wheel has a diameter of 120 metres (394 ft).</p>', -35.165034, 174.162854, 5],
      ['<h3>Whangarei</h3>' +
        '<p>The London Eye is a giant Ferris wheel situated on the banks of the River Thames. The entire structure is 135 metres (443 ft) tall and the wheel has a diameter of 120 metres (394 ft).</p>', -35.696121, 174.300132, 4],
      ['<h3>Mangawhai Heads</h3>' +
        '<p>The London Eye is a giant Ferris wheel situated on the banks of the River Thames. The entire structure is 135 metres (443 ft) tall and the wheel has a diameter of 120 metres (394 ft).</p>', -36.113032, 174.559536, 3],
      ['<h3>Auckland Hussies</h3>' +
        '<p>The London Eye is a giant Ferris wheel situated on the banks of the River Thames. The entire structure is 135 metres (443 ft) tall and the wheel has a diameter of 120 metres (394 ft).</p>', -36.852924, 174.750234, 2],
      ['<h3>Auckland</h3>' +
        '<p>The London Eye is a giant Ferris wheel situated on the banks of the River Thames. The entire structure is 135 metres (443 ft) tall and the wheel has a diameter of 120 metres (394 ft).</p>', -36.868273, 174.711450, 1]
    ];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 8,
      center: new google.maps.LatLng(-40.9006, 174.8860),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;
    var markers = new Array();

    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });

      markers.push(marker);

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }

    function AutoCenter() {
      //  Create a new viewpoint bound
      var bounds = new google.maps.LatLngBounds();
      //  Go through each...
      $.each(markers, function (index, marker) {
      bounds.extend(marker.position);
      });
      //  Fit these bounds to the map
      map.fitBounds(bounds);
    }
    AutoCenter();

  </script> 

</body>
</html>

关于javascript - Google map API v3 - 信息窗口未呈现的多个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48606966/

相关文章:

javascript - 我可以在另一个函数中使用本地 "this"吗?

android - Google Maps APIv2 身份验证失败

android - 如何使用地址和邮政编码 Android 获取 Lat Long

jquery - 在Google map 上使用blur.js(v3 API)

javascript - 将 Google basemap 添加到 ArcGIS Javascript API

javascript - 谷歌 API V3 : computeOffset function

javascript - 从字符串中十进制数字的开头和结尾删除双引号

javascript - 查找数据属性大于零的第一个元素 JQuery

javascript - 如果存在 NoScript,如何检查 NoScript(浏览器插件)并在屏幕顶​​部包含栏?

python - 使用 ijson 在 Python 中解析 GoogleMaps API Json