我编写的代码在我的位置显示标记,但是当我添加了marker.setmap方法时,地图未加载到我错了
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=ABBByyyeessssf77Z6S8c-M-T3Ea2j-uFczWXFxKh0&sensor=true">
</script>
<script type="text/javascript">
$(document).ready(function(){
$(".contact_map .load").append("Loading Map...")
});
function initialize() {
$(".contact_map .load").empty();
var mapOptions = {
center: new google.maps.LatLng(22.722337, 75.881732),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
title:"Hello World!"
});
}
google.maps.event.addDomListener(window, 'load', initialize)
marker.setMap(map);
</script>
<div class="contact_container">
<div class="contact_image">
</div>
<div class="contact_map">
<p class="load"></p>
<div id="map_canvas"></div>
</div>
</div>
答案 0 :(得分:0)
变量标记只存在于initialize函数内部,不能在外面使用它。为此,请在函数外部定义此var。另外,我没有在任何地方看到var myLatLng
var marker;
function initialize() {
//your other code code
marker = new google.maps.Marker({ //don't use the word 'var' in this line
position: myLatlng,
title:"Hello World!"
});
}
google.maps.event.addDomListener(window, 'load', initialize)
marker.setMap(map);