2

我正在研究谷歌地图 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>
4

2 回答 2

7

我想你也必须调整你的 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() );
   });

这样您就可以跟踪浏览器窗口的大小调整 :)

于 2012-07-22T09:07:47.570 回答
1

同样的事情也可以只使用 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>

于 2016-01-09T13:16:48.090 回答