【问题标题】:Resizing google map according to browser resizing根据浏览器调整大小调整谷歌地图大小
【发布时间】:2012-07-20 21:17:58
【问题描述】:

我正在研究谷歌地图 api v3。地图完美地显示在我的页面上...问题是当我调整浏览器大小时,当我加载页面时地图适合其原始大小...

加载页面时的初始状态

当我调整浏览器大小时,地图的大小仍为初始状态大小。

[代码]

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<script src="http://code.jquery.com/jquery-1.7.2.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>

<script type='text/javascript'>
var point;
var mrktx;

function mshow()
{
        $("#search_content").css("display","");
}
function mhide()
{
        $("#search_content").css("display","none");
}

function load() {

   if(navigator.geolocation)
   {
        navigator.geolocation.getCurrentPosition(ShowPosition)
   }
   else
   {
      alert("Browser does not support");
      setTimeout( function(){ window.location = "../" },500);
   }
   function ShowPosition(position)
   {
        var lat = position.coords.latitude;
        var lng = position.coords.longitude;


   var cwidth = document.getElementsByTagName("body")[0].clientWidth;
   var cheight = document.getElementsByTagName("body")[0].clientHeight;
   //alert(cwidth + ',' + cheight);
    $("#body").css("overflow","hidden");
   $("#map_canvas").css("position","absolute");
   $("#map_canvas").css("overflow","auto"); 
   $("#map_canvas").css("height",cheight);
   $("#map_canvas").css("width",cwidth);
   $("#map_canvas").css("z-index","99")
   $("#map_canvas").css("top","0");
   $("#map_canvas").css("left","0em");
   $("#top_nav").css("width",cwidth);
   $("#top_nav").css("height","8%");

   var latlng = new google.maps.LatLng(lat,lng);
   var myOptions = {
      zoom: 11,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
   };

   var map = new google.maps.Map(document.getElementById("map_canvas"), 
              myOptions);

   $('document').resize(function(){
        google.maps.event.trigger(map, 'resize');
        map.setZoom( map.getZoom() );
   });



   var myMrkrTxt = "";
   var infowindow = new google.maps.InfoWindow({ content : myMrkrTxt });      
   var myMrkr = new google.maps.Marker({position:latlng,map:map});
        google.maps.event.addListener(myMrkr,'mouseover', function(){ infowindow.open(map,myMrkrTxt); });
        google.maps.event.trigger(map, "resize");


   }

}
</script>
<style>
#top_nav
{
    position: absolute; z-index: 200; top: 0px; background-color: black;
}
#top_nav h2
{
    color: white;
}
body, html {
  height: 100%;
  width: 100%;
}



</style>
</head>
<body onload='load()'>


<div id="map_canvas"></div>
</body>
</html>

【问题讨论】:

    标签: css google-maps-api-3


    【解决方案1】:

    我猜你也必须调整 map_canvas 的大小。

    所以只需将其添加到您的 resize()

    //its maybe better to attach this handler to the window instead of the document
    $(window).resize(function(){
            $('#map_canvas').css("height",$(window).height());
            $('#map_canvas').css("width",$(window).width());
            google.maps.event.trigger(map, 'resize');
            map.setZoom( map.getZoom() );
       });
    

    因此您可以跟踪浏览器窗口的大小调整 :)

    【讨论】:

    • 我实现了这个..当我调整浏览器的大小时,它出现在这个功能中,但我的地图保持不变的宽度和高度....我也将这个$(window).height()与这个$(window).height()+'px'联系起来,但不是调整我的地图大小....
    • 我得到了解决方案....我在我的节点中编辑。在这里,我在我的代码 $("map_canvas").css("height",cheight); $("map_canvas").css("height",cheight); 中注释这些行并添加 &lt;style&gt; #map_canvas { height: 100%; width: 100%; } &lt;/style&gt; i &lt;head&gt;&lt;/head&gt;
    【解决方案2】:

    同样的事情也可以只使用 CSS 来完成。下面我举个例子,喜欢就用这个吧。

    .google-maps {
      position: relative;
      padding-bottom: 75%;
      height: 0;
      overflow: hidden;
    }
    .google-maps iframe {
      position: absolute;
      top: 0;
      left: 0;
      width: 100% !important;
      height: 100% !important;
    }
    <div class="google-maps">
      <iframe src="https://www.google.com/maps/yourmapsblah" width="765" height="500" frameborder="3" style="border:0" allowfullscreen></iframe>
    </div>

    【讨论】:

    • 链接到小提琴的好东西,但在你的答案中写下具体细节也很有帮助(例如解决问题的特定 CSS)。
    猜你喜欢
    • 2018-06-19
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 2010-10-26
    相关资源
    最近更新 更多