无法让 Google API 映射出现在 Javascript 中

Can't get Google API map to appear in Javascript

本文关键字:Javascript 映射 Google API      更新时间:2023-09-26

我正在尝试创建一个谷歌地图,其中有多个地图标记。然后,应根据这些标记的最终位置将地图居中。我已经硬编码了一些坐标以进行测试。我也已经为我的地图画布div 设置了高度和宽度。我无法让地图显示在页面上。我到处看过,看过很多例子,我的例子看起来很像这些例子。是的,我确实在我的实际代码中包含我的 API 密钥。

    <!DOCTYPE html>
<html>
  <head>
  <title>Check In Draft</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <link type="text/css" href="styles.css" rel=stylesheet />
    <h1>Where am I?</h1>
    <script type="text/javascript"
      src="https://maps.googleapis.com/maps/api/js?key=xxxxx&sensor=false">
    </script>
    <script type="text/javascript">
        function initialize() {
            var mapMarker, i;
            var locations = [
                [ 'Location 1', 42.5822, -87.8456, 3 ],
                [ 'Location 2', 42.5812, -87.8446, 2 ],
                [ 'Location 3', 42.5832, -87.8466, 1 ]
            ];
            var mapOptions = {
                mapTypeId: google.maps.MapTypeId.ROADMAP,
            };
            var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
            var infowindow = new.google.maps.InfoWindow();
            var bounds = new google.maps.LatLngBounds();
            for(i = 0; i < locations.length; i++) {             
                //Edit map marker
                mapMarker = new google.maps.Marker({
                    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                    map: map,
                    animation: google.maps.Animation.DROP
                });
                bounds.extend(mapMarker.position);
                google.maps.event.addListener(marker, 'click', (function(mapMarker, i) {
                    return function() {
                        infowindow.setContent(locations[i][0]);
                        infowindow.open(map, mapMarker);
                    }
                })(mapMarker, i));
            }
            //now fit the map to the newly inclusive bounds
            map.fitBounds(bounds);
            //(optional) restore the zoom level after the map is done scaling
            var listener = google.maps.event.addListener(map, "idle", function () {
                map.setZoom(10);
                google.maps.event.removeListener(listener);
            });
        }
        google.maps.event.addDomListener(window, 'load', initialize);
    </script>
    <div id="login-links">
        Login | Sign Up
    </div>
  </head>
  <body>
    <div id="map-canvas"/>
  </body>
</html>

这是我的样式.css页面也是。

    html 
{ 
    height: 100%;
}
body 
{ 
    height: 100%; 
    margin: 0; 
    padding: 0;
    background-color: #b0c4de;
}
h1
{
    text-align: center;
}
#map-canvas 
{ 
    height: 75%; 
    width: 75%; 
    margin-left: auto; 
    margin-right: auto
}
#login-links
{
    text-align: right;
}

提前感谢任何帮助!

这是一个问题:

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

应该是

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

另一个问题是:

 google.maps.event.addListener(marker, 'click', (function(mapMarker, i) {

应该是:

 google.maps.event.addListener(mapMarker, 'click', (function(mapMarker, i) {