响应式谷歌地图?

Len*_*een 40 google-maps responsive-design

如何从代码中制作响应式谷歌地图

<div class="map">
    <iframe>...</iframe>
</div>
Run Code Online (Sandbox Code Playgroud)

我在css中使用完整的地图

.map{max-width:100%;}
Run Code Online (Sandbox Code Playgroud)

和小设备

.map{max-width:40%;}
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

有人有想法吗?谢谢.

小智 64

将其添加到初始化函数:

<script type="text/javascript">

function initialize() {

  var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

  // Resize stuff...
  google.maps.event.addDomListener(window, "resize", function() {
    var center = map.getCenter();
    google.maps.event.trigger(map, "resize");
    map.setCenter(center); 
  });

}

</script>
Run Code Online (Sandbox Code Playgroud)

  • 如果您使用此选项,则应将其包装在`setTimemout`中,因此它不会连续运行.例如`var resizeTimeout; google.maps.event.addDomListener(window,"resize",function(){if(resizeTimeout){clearTimeout(resizeTimeout);} resizeTimeout = setTimeout(function(){/*do resizing*/},250);}) ;` (10认同)
  • 由于某种原因,当调整大小的东西被包装在setTimeout函数中时,它会停止工作. (2认同)
  • 这可能看起来很愚蠢,但如何将其添加到我的"初始化函数"中?我尝试将其添加到我的标题中,其他所有脚本都是,并且我不断收到语法错误.我应该把这段代码放在哪里? (2认同)

Nim*_*les 53

添加100%的iframe 的widthheight属性对我来说一直很有用.例如

<iframe width="100%" height="100%" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.co.in/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=+&amp;q=surat&amp;ie=UTF8&amp;hq=&amp;hnear=Surat,+Gujarat&amp;ll=21.195,72.819444&amp;spn=0.36299,0.676346&amp;t=m&amp;z=11&amp;output=embed"></iframe><br />
Run Code Online (Sandbox Code Playgroud)

  • 简单、美观的解决方案。吻 (2认同)
  • 这对于所有 iframe 都更好,例如 YouTube 嵌入等。谢谢@Nimsrules (2认同)

小智 25

没有javascript的解决方案:

HTML:

<div class="google-maps">
<iframe>........//Google Maps Code//..........</iframe>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

.google-maps {
    position: relative;
    padding-bottom: 75%; // This is the aspect ratio
    height: 0;
    overflow: hidden;
}
.google-maps iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100% !important;
    height: 100% !important;
}
Run Code Online (Sandbox Code Playgroud)

如果您有自定义地图尺寸,请删除"身高:100%!重要"


小智 9

最好使用Google Map API.我在这里创建了一个例子:http://jsfiddle.net/eugenebolotin/8m1s69e5/1/

主要功能是使用功能:

//Full example is here: http://jsfiddle.net/eugenebolotin/8m1s69e5/1/

map.setCenter(map_center);
// Scale map to fit specified points
map.fitBounds(path_bounds);
Run Code Online (Sandbox Code Playgroud)

它处理调整大小事件并自动调整大小.


小智 6

在 iframe 标记中,您可以轻松添加 width='100%' 而不是地图给您的预设值

像这样:

 <iframe src="https://www.google.com/maps/embed?anyLocation" width="100%" height="400" frameborder="0" style="border:0;" allowfullscreen=""></iframe>

Run Code Online (Sandbox Code Playgroud)


Dav*_*roa 3

检查此解决方案:http://jsfiddle.net/panchroma/5AEEV/

不过,我不能把代码归功于它,它来自于使谷歌地图 iframe 嵌入响应式的快速而简单的方法

祝你好运!

CSS

#map_canvas{
width:50%;
height:300px !important;
}
Run Code Online (Sandbox Code Playgroud)

超文本标记语言

    <!DOCTYPE html>
<html>
  <head>
    <title>Google Maps JavaScript API v3 Example: Map Simple</title>
    <meta name="viewport" content="width=device-width, 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 id="map_canvas"></div>
  </body>
</html> 
Run Code Online (Sandbox Code Playgroud)