Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
143
rated 0 times [  147] [ 4]  / answers: 1 / hits: 83949  / 14 Years ago, fri, may 14, 2010, 12:00:00

I'm switching from v2 to v3 google maps api and got a problem with gMap.getBounds() function.



I need to get the bounds of my map after its initialization.



Here is my javascript code:




var gMap;
$(document).ready(

function() {

var latlng = new google.maps.LatLng(55.755327, 37.622166);
var myOptions = {
zoom: 12,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
gMap = new google.maps.Map(document.getElementById(GoogleMapControl), myOptions);

alert(gMap.getBounds());
}
);


So now it alerts me that gMap.getBounds() is undefined.



I've tried to get getBounds values in click event and it works fine for me, but I cannot get the same results in load map event.



Also getBounds works fine while document is loading in Google Maps API v2, but it fails in V3.



Could you please help me to solve this problem?


More From » google-maps

 Answers
34

In the early days of the v3 API, the getBounds() method required the map tiles to have finished loading for it to return correct results. However now it seems that you can listen to bounds_changed event, which is fired even before the tilesloaded event:



<!DOCTYPE html>
<html>
<head>
<meta http-equiv=content-type content=text/html; charset=UTF-8/>
<title>Google Maps v3 - getBounds is undefined</title>
<script src=http://maps.google.com/maps/api/js?sensor=false
type=text/javascript></script>
</head>
<body>
<div id=map style=width: 500px; height: 350px;></div>

<script type=text/javascript>
var map = new google.maps.Map(document.getElementById(map), {
zoom: 12,
center: new google.maps.LatLng(55.755327, 37.622166),
mapTypeId: google.maps.MapTypeId.ROADMAP
});

google.maps.event.addListener(map, 'bounds_changed', function() {
alert(map.getBounds());
});
</script>
</body>
</html>

[#96788] Tuesday, May 11, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brodyfrancisi

Total Points: 1
Total Questions: 102
Total Answers: 89

Location: Marshall Islands
Member since Mon, May 31, 2021
3 Years ago
brodyfrancisi questions
;