GLatLngBounds() returns inaccurate coords

127 views
Skip to first unread message

Martin™

unread,
Feb 14, 2011, 7:03:30 AM2/14/11
to Google Maps JavaScript API v3
Hi.

I've run into problems on a map that i'm developing - wherever the map
uses the getBounds() method it's behaving with unpredictable
results...

Here's the map:
http://tinyurl.com/6xwq8lr


I'm using getBounds() to:

Pan the map to keep my custom overlay (infowindow) in view.

Remove markers not within the map bounds (plus a bit of padding).

Pan the map to display search for results if not already visible.

Display the number of markers within the map bounds.

Occasionally the map functions as expected but more often than not it
doesn't and i've traced the error to the coordinates returned by
getBounds().

I searched the group and found mention that getBounds() will not
return a value until the map has completely initialised, if the map is
not initialised then getBounds() will return null or undefined.
getBounds() is not returning null or undefined on my map - it's
returning completely inaccurate values for both south-west and north-
east coordinates.

(The debug messages will appear in Firefox console if available
otherwise a div will appear under the map containing the debug
messages).

If you load the map then click on any marker and wait 30 seconds for
the map to update now look at the debug message, for example:

debug #1: ((50.818888445280834, -179.1225677030487),
(85.10135175789982, 0.5780744218750122)) | (51.4782, -0.19447)

That's generated on line 352 in this file:
http://tinyurl.com/6f7m9p4

Lines 331 to 337 of that same file also add a marker to the map at the
south-west and north-east coords returned by getBounds().
If you now zoom the map right out you'll be able to see those markers.

The debug message is the mapBounds() south-west and north-east
coordinates followed by the marker latLng that i need to test is
within the map bounds.

The other places in the map code where getBounds() is used also return
unpredictable coordinates - but i haven't added any debug messages
there (yet).

What's going wrong?

Martin.

Rossko

unread,
Feb 14, 2011, 4:20:02 PM2/14/11
to Google Maps JavaScript API v3
> I searched the group and found mention that getBounds() will not
> return a value until the map has completely initialised, if the map is
> not initialised then getBounds() will return null or undefined.
> getBounds() is not returning null or undefined on my map - it's
> returning completely inaccurate values for both south-west and north-
> east coordinates.

Is this a variation on
http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/93bd469c78a5ca15
where the meaning of 'bounds' is open to interpretation at wide zooms?

Martin™

unread,
Feb 14, 2011, 4:45:15 PM2/14/11
to Google Maps JavaScript API v3
Thanks for the reply, i had already read the thread you linked to but
don't think that's the case here.

The link to the map i posted will geocode 'london' and fit the map to
the bounds returned by the geocoder.
It's not a zoomed out view and definitely doesn't show a wrapped view
of the earth.

This map is an update to the current version which can be found here:
http://tinyurl.com/4msm6v7

On both maps there is a More control top right, which will generate a
link to the current map view.
I just noticed that in the older version the link is generated
correctly - the link containing the map's current bounds.
On the new version of the map the link generated is incorrect - the
same inaccurate values for the map bounds showing.

I shall investigate the difference between the two maps - try to find
what i've changed in the new version to cause getBounds() to return
these inaccurate values.

Martin.

On Feb 14, 9:20 pm, Rossko <ros...@culzean.clara.co.uk> wrote:
> > I searched the group and found mention that getBounds() will not
> > return a value until the map has completely initialised, if the map is
> > not initialised then getBounds() will return null or undefined.
> > getBounds() is not returning null or undefined on my map - it's
> > returning completely inaccurate values for both south-west and north-
> > east coordinates.
>
> Is this a variation onhttp://groups.google.com/group/google-maps-js-api-v3/browse_thread/th...

Martin™

unread,
Feb 14, 2011, 7:37:58 PM2/14/11
to Google Maps JavaScript API v3
I've now added an event listener to the map:

google.maps.event.addListener($map, 'bounds_changed', function(){
console.log('bounds_changed: '+$map.getBounds());
});

It's reporting the correct map bounds by the looks of it so i plan to
change the listener:

google.maps.event.addListener($map, 'bounds_changed', function(){
$this.set('mapBounds', $map.getBounds());
});

And then bind the new mapBounds property to the various map objects
using MVC bindTo() method.
Then i can get the new mapBounds property in any of the map objects
instead of using getBounds() and it'll hopefully be accurate...

I'll post again with my results.

Martin.

Martin™

unread,
Feb 15, 2011, 2:08:41 AM2/15/11
to Google Maps JavaScript API v3
Now this is getting stranger...

I created an event listener:

google.maps.event.addListener($map, 'bounds_changed', function(){
$this.set('mapBounds', $map.getBounds());
console.log('mapBounds set: '+$this.get('mapBounds'));
});

Example output:
mapBounds set: ((51.262282903037544, -0.8062029218750077),
(51.790294654209234, 0.5780744218750122))

In the code that executes when the link to the map is created (More
control):

var $bounds=this.get('mapBounds');
console.log('createCode(): '+$bounds);

Outputs:
createCode(): ((50.37387126345161, -179.8256927030487),
(85.10135192968205, 0.5780744218750122))

The map has not been panned or zoomed and yet the LatLngBounds object
has changed!!

However this DOES work:

google.maps.event.addListener($map, 'bounds_changed', function(){
var $mapBounds=$map.getBounds();
$mapBounds=new google.maps.LatLngBounds($mapBounds.getSouthWest(),
$mapBounds.getNorthEast());
$this.set('mapBounds', $mapBounds);
console.log('mapBounds set: '+$this.get('mapBounds'));
});

var $bounds=this.get('mapBounds');
console.log('createCode(): '+$bounds);

The output of the last two console.log() messages is the SAME.
So i'll use this method - i haven't got time to debug what the
original problem was.

Martin.

Martin™

unread,
Feb 15, 2011, 3:20:11 AM2/15/11
to Google Maps JavaScript API v3
I was wrong - the code in my previous post didn't work.
The LatLngBounds object not retaining the correct values.

I'm experimenting now with this:

google.maps.event.addListener($map, 'bounds_changed', function(){
var $mapBounds=$map.getBounds();
$this.set('mapBounds', {sw:$mapBounds.getSouthWest(), ne:
$mapBounds.getNorthEast()});
});

Then when i need the map's current bounds i create a new LatLngBounds
object from the two LatLngs.
It seems to work but needs a thorough test before i'm sure it's bug
free...

Martin.
Reply all
Reply to author
Forward
0 new messages