看不到Google标记
问题描述:
有人可以告诉我为什么我在这张地图上看不到标记吗?
Can someone tell me why I can't see the marker on this map?
<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>
答
在浏览器的错误控制台中查看:
Look in your browser's error console:
Uncaught ReferenceError: map is not defined
您需要公开该变量,因为map
当前在initialize()
范围之外不可见.或者,将标记代码移到initialize()
:
You need to expose that variable, since map
is not currently visible outside of the scope of initialize()
. Or, move the marker code into 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
});
}