我正在尝试将Google API中的地图加载到div中。但是,未加载地图,并且不输出错误。这是代码:
// google maps
var geocoder, map;
function codeAddress(address) {
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var myOptions = {
zoom: 8,
center: results[0].geometry.location,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
}
});
}
google.maps.event.addDomListener(window, 'load', function() {
codeAddress('" . $location['address'] . " " . $location['zip'] . " " . $location['city'] . " " . $countries[$location['country']] . "');
});
我已经在很长一段时间内处理这个问题了,而且我没有想法。
您是否熟悉google maps api,谁知道我的代码有什么问题?我的div确实存在ID:map_canvas。
答案 0 :(得分:5)
问题是:
我没有设置div(.map_canvas {width: 500px; height: 500px;}
)的宽度/高度。
我忘记了这样的事情:
initialize();
function initialize() {
google.maps.event.addDomListener(window, 'load', function() {
codeAddress('London');
});
}
它现在有效。谢谢!
答案 1 :(得分:1)
您是否正在加载Google Maps API ...?你不是在网页上遗漏了这样的东西吗?
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=<your_api_key&sensor=false&callback=initialize"></script>
...
<script>
function initialize() {
codeAddress('" . $location['address'] . " " . $location['zip'] . " " . $location['city'] . " " . $countries[$location['country']] . "');
});
</script>
请查看Google's example。
答案 2 :(得分:0)
我假设你在地图上加载了地图&#39; map_canvas&#39; div,对吗?尝试在样式表上定义这些div的高度和宽度,这就是我在项目中发生的事情并且它为我解决了......问题似乎是因为其他样式属性,如位置或漂浮可能。