javascript - 访问时 map 不显示

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

我有这个 HTML 文件(如下),它与此处找到的文件几乎相同。 https://developers.google.com/maps/documentation/javascript/examples/infowindow-simple

我遇到的问题是我认为任何脚本部分或 HTML 都没有任何问题,然后我再次作为志愿者为非营利组织编写此内容,并且我通常不会执行此 HTML 或 javascript 。该文件是由 perl 脚本生成的,所以请对我宽容一些。

请让我知道我做错了什么!

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>e-NABLE Event Map</title>
    <style>
        html, body, #map-canvas {
            height: 100%;
            margin: 0px;
            padding: 0px
        }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
    <script>

    function initialize() {
        var myHome = new google.maps.Latlng(-77.675106,43.090076);
        var mapOptions = {
            zoom: 4,
            center: myHome
        };

        var map = new google.maps.Map(document.getElementByID('map-canvas'), mapOptions);

        var contentString = '<div id="content">'+
          '<div id="siteNotice">'+
          '</div>'+
          '<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
          '<div id="bodyContent">'+
          '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
          'sandstone rock formation in the southern part of the '+
          'Northern Territory, central Australia. It lies 335&#160;km (208&#160;mi) '+
          'south west of the nearest large town, Alice Springs; 450&#160;km '+
          '(280&#160;mi) by road. Kata Tjuta and Uluru are the two major '+
          'features of the Uluru - Kata Tjuta National Park. Uluru is '+
          'sacred to the Pitjantjatjara and Yankunytjatjara, the '+
          'Aboriginal people of the area. It has many springs, waterholes, '+
          'rock caves and ancient paintings. Uluru is listed as a World '+
          'Heritage Site.</p>'+
          '<p>Attribution: Uluru, <a href="http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
          'http://en.wikipedia.org/w/index.php?title=Uluru</a> '+
          '(last visited June 22, 2009).</p>'+
          '</div>'+
          '</div>';

        var myLatlng0 = new google.maps.LatLng(-77.675106,43.090076);

        var infowindow0 = new google.maps.InfoWindow({
            content: contentString
        });

        var marker0 = new google.maps.Marker({
            position: myLatlng0,
            map: map,
            title: 'e-NABLE Home Base'
        });

        google.maps.event.addListener(marker0, 'click', function() {
            infowindow.open(map,marker0);
        });

    }

    google.maps.event.addDomListener(window, 'load', initialize);

    </script>
</head>

<body>
    <div id="map-canvas"></div>
</body>

最佳答案

JavaScript 区分大小写。 new google.maps.Latlng(-77.675106,43.090076) 应该是 new google.maps.LatLng(-77.675106,43.090076)document.getElementByID( 'map-canvas') 应为 document.getElementById('map-canvas')。就我个人而言,我不喜欢输入 google.maps 或使用 document.getElementById(),所以我这样做:

var doc = document, bod = document.body, gm = google.maps;
function E(e){
  return doc.getElementById(e);
}
var myHome = new gm.LatLng(-77.675106,43.090076);
var canvas = E('map-canvas'); // same as document.getElementById('map-canvas');

关于javascript - 访问时 map 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26209427/

相关文章:

python - 如何修复 "googlemaps.exceptions.ApiError : REQUEST_DENIED"

javascript - 谷歌地图API放置自动填充表单onload

javascript - "place"在此 for 循环中意味着什么

javascript - 让 JEditable 处理新元素 (.live)

javascript - Google map - 从 Google map 获取省份轮廓

json - Google 我的 map - JSON 格式的数据层

google-maps - 正/负纬度和经度值与基本方向

angular - 有没有办法在 AGM map 中设置边界和缩放级别?

javascript - window.location 改变时中断?

javascript 返回两个单选按钮值