自定义Google地图不会显示在div元素中 [英] Custom Google Map won't show in div element

查看:90
本文介绍了自定义Google地图不会显示在div元素中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是css代码:

 #map-canvas {
width:100%;
height:500px;
}

和js代码:

 < script src =https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false>< /脚本> 
< script>
函数initialize(){
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var mapOptions = {
zoom:4,
center:myLatlng
}
var map = new google.maps.Map(document.getElementById('map-canvas '),mapOptions);

var marker = new google.maps.Marker({
position:myLatlng,
map:map,
title:'Hello World!'
});
}

google.maps.event.addDomListener(window,'load',initialize);
< / script>

这就是我如何在html中初始化映射。

 < div id =map-canvas>< / div> 

但是,当我尝试将我的地图放入某个div时,例如:

 < div> 
< div id =map-canvas>< / div>
< / div>

它不起作用。如何解决这个问题?



///编辑
请把它放到example.html中,然后尝试运行。没有任何反应。

 <!DOCTYPE html> 
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8>

< style>
html,body,#map-canvas {
height:100%;
margin:0px;
填充:0px
}
< / style>

< script src =https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false>< / script>
< script>
函数initialize(){
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var mapOptions = {
zoom:4,
center:myLatlng
}
var map = new google.maps.Map(document.getElementById('map-canvas '),mapOptions);

var marker = new google.maps.Marker({
position:myLatlng,
map:map,
title:'Hello World!'
});
}

google.maps.event.addDomListener(window,'load',initialize);
< / script>
< / head>

< body>
< div>
< div id =map-canvas>< / div>
< / div>
< / body>
< / html>


解决方案

我已检查和测试。



DEMO


I'm trying to customize google map but I have a small problem.

Here is the css code:

#map-canvas {
    width: 100%;
    height: 500px;
}

and the js code:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
    function initialize() {
      var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
      var mapOptions = {
        zoom: 4,
        center: myLatlng
      }
      var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

      var marker = new google.maps.Marker({
          position: myLatlng,
          map: map,
          title: 'Hello World!'
      });
    }

    google.maps.event.addDomListener(window, 'load', initialize);
</script>

And this is how I initialize the map in html. This example actually works.

<div id="map-canvas"></div>

But when I trying to put my map into some div e.g:

<div>
    <div id="map-canvas"></div>
</div>

It doesn't work. How to fix that ?

/// EDIT Please put this into example.html and try to run. Nothing happens for me.

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>

    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
        function initialize() {
          var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
          var mapOptions = {
            zoom: 4,
            center: myLatlng
          }
          var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

          var marker = new google.maps.Marker({
              position: myLatlng,
              map: map,
              title: 'Hello World!'
          });
        }

        google.maps.event.addDomListener(window, 'load', initialize);
    </script>
</head>

<body>
    <div>
        <div id="map-canvas"></div>
    </div>
</body>
</html>

解决方案

Nothing seems wrong in your code ,it works fine just seprated css.I have checked and tested.

DEMO

这篇关于自定义Google地图不会显示在div元素中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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