[英]Google Map doesn't appear (doesn't load)
I don't have experience with google map API, so I hope you can help me. 我没有使用Google Map API的经验,所以希望您能对我有所帮助。
the problem is whenever I click on show button, the width of the map appear, but it doesn't load, I can't see the map at all. 问题是,每当我单击“显示”按钮时,地图的宽度就会出现,但不会加载,我根本看不到地图。
but if i open the counsel in developer tools the map loads normally! 但是,如果我在开发人员工具中打开建议,则地图会正常加载!
codepen url for better explanation https://codepen.io/ahmadz12/pen/QdRQbE Codepen网址以获得更好的解释https://codepen.io/ahmadz12/pen/QdRQbE
html html
<div id="map-canvas" class='hide'></div>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false" type="text/javascript"></script>
<a id='show'>show</a>
css 的CSS
#map-canvas{
height: 500px;
width: 500px;
border: 3px solid black;
}
.hide{
display:none;
}
javascript javascript
if (document.getElementById('map-canvas')){
// Coordinates to center the map
var myLatlng = new google.maps.LatLng(52.525595,13.393085);
// Other options for the map, pretty much selfexplanatory
var mapOptions = {
zoom: 14,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// Attach a map to the DOM Element, with the defined settings
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
}
$('#show').click(function(){
$('#map-canvas').removeClass( "hide" )
});
Thanks 谢谢
When you load a Google map into a hidden div and then display that div, you need to tell Google to repaint the map: 当您将Google地图加载到隐藏的div中并显示该div时,您需要告诉Google重新绘制地图:
$('#show').click(function(){
$('#map-canvas').removeClass( "hide" );
google.maps.event.trigger(map, 'resize');
});
I suspect that Google is trying to be helpful by saving browser resources, not drawing maps in elements that can't be seen. 我怀疑Google试图通过节省浏览器资源而不是在看不见的元素中绘制地图来提供帮助。 By triggering a resize when you display your div, it causes Google to draw the map again in a now visible element.
在显示div时触发调整大小,这会导致Google在现在可见的元素中再次绘制地图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.