无法让Google API地图显示在Javascript中

时间:2013-11-14 22:03:26

标签: java javascript html google-maps-api-3

我正在尝试创建一个Google地图,其中我有多个地图标记。然后应根据这些标记的最终位置使地图居中。为了测试目的,我对一些坐标进行了硬编码。我已经为我的map-canvas 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;
}

提前感谢您的帮助!

1 个答案:

答案 0 :(得分: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) {