简体   繁体   English

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

[英]Why don't my Google Maps Markers load?

I'm new to JavaScript and the Google Maps API v3. 我是JavaScript和Google Maps API v3的新手。 I'm trying the Developer's Guide sample code in an ASP.Net page. 我正在ASP.Net页面中尝试开发人员指南示例代码。 The map displays as expected, but the marker doesn't load. 地图按预期显示,但标记未加载。 I'd appreciate it if someone could spot my mistake: 如果有人能发现我的错误,我会很感激:

<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>

Try putting the Marker code inside the initialize function. 尝试将标记代码放在初始化函数中。 Otherwise the marker doesn't see the created map. 否则标记不会看到创建的地图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM