map.getBounds() don't work

7,119 views
Skip to first unread message

APh

unread,
Nov 7, 2009, 9:15:50 PM11/7/09
to Google Maps JavaScript API v3
Hi ALL!

I'm try to type bounds of my map via alert() , but can't. <a
href="http://r-komfort.ru/test/v3_bounds.html">[ link ]</a>

IE8 message: 'undefined' for code string ".getBounds()".

<a href="http://code.google.com/apis/maps/documentation/v3/
reference.html#Map">Documentation</a> say:
"If the map is not yet initialized (i.e. the mapType is still null),
or center and zoom have not been set then the result is null."

But I create a map object in this way:
<pre>
var latlngMoscow = new google.maps.LatLng(55.877648, 37.681915);
var myMoscowMapOptions = {
zoom: 15,
center: latlngMoscow,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

var mapMoscow = new google.maps.Map(document.getElementById
("myMoscowMap"), myMoscowMapOptions);
</pre>

The string "zoom: 15" is setting zoom or not?

What's wrong?

Thx in advance!

--= APh =--

String

unread,
Nov 8, 2009, 6:43:23 AM11/8/09
to Google Maps JavaScript API v3
It's almost definitely a timing issue.

My first guess is that at the point where you call getBounds(), the
page is not yet fully rendered, so the map doesn't know what its
bounds are going to be yet. This is because you're calling your
initialize function inline, rather than attaching it to the body's
onload event, or otherwise waiting until the page is done loading.

I'd also suspect that, since v3 doesn't load resources until it needs
them, getBounds() may not be able to be called so soon after
initializing the map. You may need to move it into an event which
fires after the map has finished loading. But that seems less likely;
look at my previous suggestion first.

Good luck with it,

String

yunhui song

unread,
Nov 8, 2009, 2:01:18 AM11/8/09
to google-map...@googlegroups.com
use tileloaded event handler, for chrome, you need setTimeout(function(), 1000).

//when all tiles are loaded, add the Markers,it depends on bounds of the map
var boundsChangedListener = google.maps.event.addListener(map,
'tilesloaded', function() {
//Chrome need timeout!?
setTimeout(function(){
addMarkers(map);
},1000);
//addMarkers(map);
});

APh

unread,
Nov 8, 2009, 11:05:25 PM11/8/09
to Google Maps JavaScript API v3
Hi String and ALL!
My inline call of initialize() is the LAST operator before </body> tag
and I hope that this is equal to
string <body onLoad="initialize()">. It was my experiment...
BUT I'm try both variants! Result is the same - getBounds() don't
work.
It was yesterday...

Today I make <body onLoad="initialize()"> version and getBounds()
WORK!!!
Without any special efforts!
Later 10 min. don't work again. Now work... Paradox!
It so unstable!

Situation described above is for Chrome. IE8 show message that
getBounds() is not defined as before.

I think that there can be initialize() has a certain magic value. And
all resources become accessible already after completion of this
function.
When I have taken out my business logic (alert("Center...") and alert
("Bounds...") ;) for limits of a body of function initialize(), all
began to work remarkably!

Thx to all!
R.,
Alex Philippov

APh

unread,
Nov 8, 2009, 11:12:29 PM11/8/09
to Google Maps JavaScript API v3
Hi yunhui song and ALL!
It's an interesting way.
I tried it.

However the given event (tilesloaded) occurs later, than function
initialize () is completed. At least for me...

But I will add your advice to the arsenal.

Thanks!

R.,
Alex Philippov

Andre Tannus

unread,
Nov 20, 2009, 9:32:27 PM11/20/09
to Google Maps JavaScript API v3
Hey APh, here´s what I´ve found:

if you call getBounds() from withing the initialize function, nothing
is returned (possibly because no map has been initialized?).
Even if you set you <body>´s onlick like this

onClick='initialize(); get_me_bounds();'

where get_me_bounds() is a function that calls getBounds(), undefined
is returned, but one out of a hundred times, it returns bounds!

So I tried this:

onClick='initialize(); setTimeout('get_me_bounds()', 100);'

which delays the call for get_me_bounds() 100 miliseconds, and IT
WORKS!

I think the API is not yet done processing the map construct, so you
have to give it a breath to finish up.

Let me know if this is too crazy.




On Nov 9, 2:05 am, APh <gis_n_...@yahoo.com> wrote:
> Hi String and ALL!
> My inline call of initialize() is the LAST operator before </body> tag
> and I hope that this is equal to
> string <body onLoad="initialize()">. It was my experiment...
> BUT I'm try both variants! Result is the same -getBounds() don't
> work.
> It was yesterday...
>
> Today I make <body onLoad="initialize()"> version andgetBounds()
> WORK!!!
> Without any special efforts!
> Later 10 min. don't work again. Now work... Paradox!
> It so unstable!
>
> Situation described above is for Chrome. IE8 show message thatgetBounds() is not defined as before.
>
> I think that there can be initialize() has a certain magic value. And
> all resources become accessible already after completion of this
> function.
> When I have taken out my business logic (alert("Center...") and alert
> ("Bounds...") ;) for limits of a body of function initialize(), all
> began to work remarkably!
>
> Thx to all!
> R.,
>   Alex Philippov
>
> On 8 ноя, 14:43, String <sterling.ud...@googlemail.com> wrote:
>
>
>
> > It's almost definitely a timing issue.
>
> > My first guess is that at the point where you callgetBounds(), the
> > page is not yet fully rendered, so the map doesn't know what its
> > bounds are going to be yet. This is because you're calling your
> > initialize function inline, rather than attaching it to the body's
> > onload event, or otherwise waiting until the page is done loading.
>
> > I'd also suspect that, since v3 doesn't load resources until it needs
> > them,getBounds() may not be able to be called so soon after

Susannah (Google Employee)

unread,
Nov 23, 2009, 7:11:39 PM11/23/09
to Google Maps JavaScript API v3
Hi,

The bounds is not calculated until the map is fully initialized or
updated. The bounds is only available asynchronously after a call to
setCenter or setZoom. You should listen to the 'bounds_changed' event
on the map before calling getBounds(). You may also find the 'idle'
event useful.

Here is an excerpt from the conceptual documentation:
Note: If you are trying to detect a change in the viewport, be sure to
use the specific bounds_changed event rather than constituent
zoom_changed and center_changed events. Because the Maps API fires
these latter events independently, getBounds() may not report useful
results until after the viewport has authoritatively changed. If you
wish to getBounds() after such an event, be sure to listen to the
bounds_changed event instead.

I hope this helps.
-Susannah

CTAPbIu_MABP

unread,
Jan 22, 2010, 2:56:49 PM1/22/10
to Google Maps JavaScript API v3
Hi,

As I understant you think that getBounds is only usefull when I move/
zoom my map
But I want to know when my marker is visible

var map = new google.maps.Map(document.getElementById("map"), {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.HYBRID
});

var marker = new google.maps.Marker({
position: new google.maps.LatLng(-34.397, 150.644),
map: map,
title:"Hello World!"
});

var isVisible = map.getBounds().contains(marker.getPosition());

How can I do this?

Reply all
Reply to author
Forward
0 new messages