谷歌地图不起作用我已经完成了这段代码。但是当我在另一个项目中使用相同的代码时,它无法正常工作。 lat long值很好。
<div class="row">
<div class="col-md-12">
<div id="googleMap" style="height: 354px; width:200px;"></div>
</div>
</div>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBWT14OcdohKHZ1i-BmHEETzm6DUskY8Cg" type="text/javascript"></script>
<script>
var lat = '<?php echo $lat?>';
var long = '<?php echo $longi?>';
var myCenter = new google.maps.LatLng(lat, long);
var marker;
function initialize()
{
var mapProp = {
center: myCenter,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
var marker = new google.maps.Marker({
position: myCenter,
animation: google.maps.Animation.BOUNCE
});
marker.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
});
</script>
&#13;
答案 0 :(得分:0)
如果我从API中删除async defer
,则发布的代码有效:
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBWT14OcdohKHZ1i-BmHEETzm6DUskY8Cg" type="text/javascript"></script>
(您应该删除它们,或者在包含中添加&callback=initialize
并删除在加载时运行addDomListener
的{{1}}
顺便说一句 - 您还有一个语法错误,一个额外的initialize
)
});
var lat = 42;
var long = -72;
var myCenter = new google.maps.LatLng(lat, long);
var marker;
function initialize() {
var mapProp = {
center: myCenter,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
var marker = new google.maps.Marker({
position: myCenter,
animation: google.maps.Animation.BOUNCE
});
marker.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);