看不到谷歌标记

时间:2012-09-11 18:14:55

标签: marker

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

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

1 个答案:

答案 0 :(得分: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
    }); 
}