为什么不加载我的Google地图标记?

时间:2012-05-05 22:47:22

标签: javascript google-maps-api-3 google-maps-markers

我是JavaScript和Google Maps API v3的新手。我正在ASP.Net页面中尝试开发人员指南示例代码。地图按预期显示,但标记未加载。如果有人能发现我的错误,我会很感激:

<style type="text/css">
    html, body, #map_canvas { margin: 0; padding: 0; height: 100%; }
</style>
<script type="text/javascript"
    src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
    var map;
    var myLatlng = new google.maps.LatLng(-41.2954168187213,174.767133718655 )
    function initialize() {
        var myOptions = {
            zoom: 4,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
    }

    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title: "Hello World!"
    });
    // The following set map made no difference.
    //The constructor above should have been enough?
    //marker.setMap(map);
    //google.maps.event.addDomListener(window, 'load', initialize);
</script>
<body  onload="initialize()">
    <div id="map_canvas"></div>
</body>

1 个答案:

答案 0 :(得分:2)

尝试将标记代码放在初始化函数中。否则,标记不会看到创建的地图。