I am initializing a google maps in a display none div and something is wrong, I only see a grey square
<div style="display:none;" id="divMap">
<div id="map" style="width:300px; height:300px;"></div>
</div>
<button onclick="showMap();">test</button>
but when i remove style="display:none;" it works
I have created a fiddler https://jsfiddle.net/ka8nywrg/2/ you can test it from here. maps is shown when you press the button
what should i do to prevent this situation?
code snippet (from posted fiddle):
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
}
function showMap(){
document.getElementById("divMap").style.display = 'block'
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
<div style="display:none;" id="divMap">
<div id="map" style="width:300px; height:300px;"></div>
</div>
<button onclick="showMap();">show map</button>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap" async defer></script>