在div内部,Google地图不起作用

pup*_*eno 8 javascript api google-maps

我正在尝试使用Google Maps API,并且包含地图的div仅在不在另一个div内时才有效.我已经创建了一个带有两个地图的示例代码,第一个是第二个没有.如果我删除这段代码的doctype,两者都有效.有什么想法吗?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta content='application/xhtml+xml; charset=UTF-8' http-equiv='content-type' />
    <style type='text/css'>
        html { height: 100% }
        body { height: 100%; margin: 0px; padding: 0px }
        #map_canvas { height: 50% }
        #map_canvas2 { height: 50% }
    </style>
    <title>Map</title>
    <script src='http://maps.google.com/maps/api/js?sensor=false' type='text/javascript'></script>
    <script type='text/javascript'>
        function initialize() {
          var latlng = new google.maps.LatLng(20, 20);
          var myOptions = {
          zoom: 8,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        var map2 = new google.maps.Map(document.getElementById("map_canvas2"), myOptions);
        }
    </script>
  </head>
  <body onload='initialize()'>
    <div id='map_canvas'></div>
    <div>
      <div id='map_canvas2'></div>
    </div>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

这就是它在Firefox和Chrome中的样子:

替代文字

替代文字


ekh*_*led 5

它确实有效,如果你在firebug中检查页面,你会看到正在创建地图.但由于您没有将CSS定位应用于外部div,因此Google地图创建的地图div位于第一个map_canvas下

你需要定位和设置外部div的尺寸,尝试给外部div一个像素高度和像素宽度......

编辑

代替 :

<div id='map_canvas'></div>
<div>
  <div id='map_canvas2'></div>
</div>
Run Code Online (Sandbox Code Playgroud)

尝试:

<!--<div id='map_canvas'></div>-->
<div style="width:900px;height:900px;">
  <div id='map_canvas2'></div>
</div>
Run Code Online (Sandbox Code Playgroud)

你会看到地图正在运作......


mat*_*pol 0

我认为它们坐在彼此之上或其他东西 - 将 css 上的高度更改为像素高度,两个地图都可以工作