0

谁能告诉我为什么我在这张地图上看不到标记?

<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
    html { height: 100% }
    body { height: 100%; margin: 0; padding: 0 }
    #map_canvas { height: 100% }
</style>

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?keykey=AIzaSyBG47z_ebM8I6Ic2-rXL8QiPN5CHCOBwco&sensor=true"></script>
<script type="text/javascript">
    function initialize() {
        var mapOptions = {
            center: new google.maps.LatLng(37.998727,-0.686259),
            zoom: 18,
            minZoom: 4,
            maxZoom: 13,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            streetViewControl: false
        }; 
        var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 
    }     

    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(37.998727,-0.686259), 
        map: map
    });
</script>
<body onload="initialize()">
<div id="map_canvas" style="width:600px; height:600px;"></div>
4

1 回答 1

1

查看浏览器的错误控制台:

Uncaught ReferenceError: map is not defined 

您需要公开该变量,因为map当前在initialize(). 或者,将标记代码移动到initialize()

function initialize() {
    var mapOptions = {
        center: new google.maps.LatLng(37.998727,-0.686259),
        zoom: 18,
        minZoom: 4,
        maxZoom: 13,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        streetViewControl: false
    }; 
    var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(37.998727,-0.686259), 
        map: map
    }); 
}     
于 2012-09-11T18:22:08.187 回答