当嵌套在div标记中时,地图不会显示在Google Maps JavaScript API v3上 [英] Map isn't showing on Google Maps JavaScript API v3 when nested in a div tag

查看:86
本文介绍了当嵌套在div标记中时,地图不会显示在Google Maps JavaScript API v3上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图放置显示地图的 div 标签(< div id =map-canvas>< / div> )在另一个 div 内,但它不以那种方式显示地图。它是一个CSS或JavaScript问题?或者它只是API的工作方式?



这里是我的嵌套 div 的代码:

 <!DOCTYPE html> 
< html>
< head>
< title>简易地图< /标题>
< meta name =viewportcontent =initial-scale = 1.0,user-scalable = no>
< meta charset =utf-8>
< style>
html,body,#map-canvas {
margin:0;
padding:0;
身高:100%;
}
< / style>
< script src =https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false>
< / script>
< script>
var map;
函数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将显示地图 - >
< div id =map-canvas>< / div>
< / div>
< / body>
< / html>


解决方案

添加 style =width :100%;身高:100%;给div看看有什么作用

不是 #map_canvas 但主分区



示例

 <身体GT; 
< div style =height:100%; width:100%;>
< div id =map-canvas>< / div>
< / div>
< / body>

这里还有其他一些答案,解释为什么这是必要的

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>

解决方案

Add style="width:100%; height:100%;" to the div see what that does

not to the #map_canvas but the main div

example

<body>
    <div style="height:100%; width:100%;">
         <div id="map-canvas"></div>
    </div>
</body> 

There are some other answers on here the explain why this is necessary

这篇关于当嵌套在div标记中时,地图不会显示在Google Maps JavaScript API v3上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆