简体   繁体   中英

Why don't my Google Maps Markers load?

I'm new to JavaScript and the Google Maps API v3. I'm trying the Developer's Guide sample code in an ASP.Net page. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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