Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

css - Resizing google map according to browser resizing

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 pageenter image description here

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

enter image description here

[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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

i guess you have to resize your map_canvas as well.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...