我正在研究谷歌地图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>
解决方法
我想你也必须调整你的map_canvas.
所以只需将其添加到您的调整大小()
//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() ); });
所以你跟踪你的浏览器窗口的大小调整:)