我正在尝试将div用于显示地图(<div id="map- canvas"></div>)的标签放在另一个标签中div,但它不会以这种方式显示地图。是CSS还是JavaScript问题?还是仅仅是API的工作方式?
div
<div id="map- canvas"></div>
这是带有嵌套的代码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>
添加style="width:100%; height:100%;"到div看看有什么用
style="width:100%; height:100%;"
不是到#map_canvas主分区
#map_canvas
例
<body> <div style="height:100%; width:100%;"> <div id="map-canvas"></div> </div> </body>
这里还有其他答案,解释了为什么这样做是必要的