根据浏览器调整大小调整google地图大小 [英] Resizing google map according to browser resizing

查看:175
本文介绍了根据浏览器调整大小调整google地图大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用google地图api v3。地图是完美地显示在我的页面...问题是,当我调整浏览器大小,映射适合其原始大小,当我加载页...

i am working on google map api v3. map is perfectly showing on my page... problem is that when i resize the browser, map fit to its original size when i load the page...

初始状态当我加载页面

initial state when i load the page

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

when i resize the browser, map is still sized at initial state size.

[Code]

<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>


推荐答案

我想你必须调整map_canvas的大小。

i guess you have to resize your map_canvas as well.

所以只需将它添加到resize()

so just add this to your 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() );
   });

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

so you have track of the resizing of your browserwindow :)

这篇关于根据浏览器调整大小调整google地图大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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