I'm trying to put the div
tag that shows the map (<div id="map-canvas"></div>
) inside another div
, but it doesn't show the map that way. Is it a CSS or a JavaScript problem? Or is it just the way the API works?
Here's my code with the the nested div
:
<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
margin: 0;
padding: 0;
height: 100%;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false">
</script>
<script>
var map;
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div> <!-- ommiting this div will show the map -->
<div id="map-canvas"></div>
</div>
</body>
</html>
Wizard
Have you tried setting the height and width of the extra div, I know that on a project I am working on JS won't put anything in the div unless I have the height and width already set.
I used your code and hard coded the height and width and it shows up for me and without it doesn't show.
I would recommend either hard coding it in or assigning the div an ID and then add it to your CSS file.
I solved this problem with:
Add
style="width:100%; height:100%;"
to the div see what that doesnot to the
#map_canvas
but the main divexample
There are some other answers on here the explain why this is necessary
The problem is with percentage sizing. You are not defining the size of the parent div (the new one), so the browser can not report the size to the Google Maps API. Giving the wrapper div a specific size, or a percentage size if the size of its parent can be determined, will work.
See this explanation from Mike Williams' Google Maps API v2 tutorial:
Add the 100% size to html and body in your css
Add it inline to any divs that don't have an id:
FIXED
Give initial height of div and specify height in percentage in your style;
I just want to add what worked for me, I added height and width to both divs and used bootstrap to make it responsive
in order for
col-lg-1
to work add bootstrap reference located Here