Dynamic zoom level to fit markers

2,692 views
Skip to first unread message

GG

unread,
Nov 14, 2009, 2:52:24 PM11/14/09
to Google Maps JavaScript API v3
I need to dynamically adjust the zoom level to make sure all markers
are visible on the map.
Can this be done in v3?

Similar to this but using v3:
http://econym.org.uk/gmap/basic14.htm

String

unread,
Nov 15, 2009, 2:15:19 AM11/15/09
to Google Maps JavaScript API v3
You use exactly the same approach in v3 (modified for the changes in
class & method names, of course). Here's the LatLngBounds class to
use:
http://code.google.com/apis/maps/documentation/v3/reference.html#LatLngBounds

String

Gary Little

unread,
Nov 14, 2009, 10:25:29 PM11/14/09
to Google Maps JavaScript API v3
You can use the V3 map's fitBounds method to do this.

Gary

GG

unread,
Nov 15, 2009, 7:34:35 PM11/15/09
to Google Maps JavaScript API v3
Ah fitBounds() is what I was looking for. Thanks Gary.

A new issue with that though...
The two coordinates I am trying to fit are random.
How do I determine their relation to one another?
In order for LatLngBounds to work I need to know which is SW and which
is NE.
Then I can put them in the correct order.

fitBounds(bounds:LatLngBounds)
LatLngBounds(sw?:LatLng, ne?:LatLng)

String

unread,
Nov 16, 2009, 2:37:47 AM11/16/09
to Google Maps JavaScript API v3
On Nov 16, 12:34 am, GG <gerber.g...@gmail.com> wrote:

> The two coordinates I am trying to fit are random.
> How do I determine their relation to one another?
> In order for LatLngBounds to work I need to know which is SW and which
> is NE.

No, you don't. Use LatLngBounds like this:

var bounds = new LatLngBounds();
bounds.extend(point1);
bounds.extend(point2);

Then the points' relation to each other doesn't matter.

Incidentally, unless your points span the 180-degree longitude line,
figuring out their relationship is a pretty trivial matter of
comparing their latitude & longitude values...

String
Reply all
Reply to author
Forward
0 new messages