0

我正在尝试创建一个谷歌地图,其中有多个地图标记。然后应根据这些标记的最终位置将地图居中。我为测试目的硬编码了一些坐标。我也已经为我的地图画布 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>

这也是我的styles.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;
}

提前感谢您的帮助!

4

1 回答 1

1

这是一个问题:

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) {
于 2013-11-14T22:28:02.857 回答