谷歌地图没有出现

时间:2013-03-30 12:34:05

标签: javascript function google-maps

谷歌地图没有出现。我认为问题是lat和long的数据类型。这是代码: 当提供lat long的硬编码值时,所有函数都能正常工作。

function initialize(a, b, zoom) {
    alert('init called  ' + "lat: " + typeof a + "long: " + typeof b + "zoom: " + typeof zoom);
    //here lat long are string and zoom is number 

    //var zoom =parseInt(zoom);
    var a = parseFloat(a);
    var b = parseFloat(b);
    alert('init called  ' + "lat: " + typeof a + "long: " + typeof b + "zoom: " + typeof zoom);

    //here lat oang and zoom all are number
    var centerLoc = new google.maps.LatLng(a, b);

    var mapProp = {
        center: centerLoc,
        zoom: zoom,
        //   mapTypeId:google.maps.MapTypeId.ROADMAP,
        mapTypeId: google.maps.MapTypeId.SATELLITE
    };
    var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
    marker = new google.maps.Marker({
        position: centerLoc,
        title: 'Click to zoom'
    });
    google.maps.event.addListener(marker, 'click', function () {
        map.setZoom(map.getZoom() + 1);
        map.setCenter(marker.getPosition());
    });
    marker.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);

</script>

这是html:

<html>
<body>
<div id="googleMap" style="width:1000px;height:500px;"></div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您的代码适用于我。唯一的问题是你做不到:

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

因为您无法将参数传递给以这种方式调用的函数。如果你在body标签中使用它作为onload函数,它可以工作:

<body onload="initialize(-33.8665433, 151.1956316,5)">

Working example