[Google Maps API v3] How can I set the zoom level based on distance?

14,744 views
Skip to first unread message

The Net Duck

unread,
Apr 22, 2010, 7:32:46 PM4/22/10
to Google Maps JavaScript API v3
HI
I'm trying to zoom my map in our out based on the distance my users
set. For instance, a user might say they want to see a map with
objects around them at 50 miles or maybe 20 miles etc. I want the
google map to only zoom out to 25 miles from their given point.

I have their given point but I don't know how to manipulate the zoom
level in such a way that they only see X distance from their current
point.

Thanks!

- The Net Duck

--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.

Esa

unread,
Apr 22, 2010, 10:03:51 PM4/22/10
to Google Maps JavaScript API v3
You can use Circle() as a helper to avoid maths.

Create an instance of Circle() with 25 miles radius.

var circle = new Circle({radius: 40234, center: thePoint});

Center and zoom the map to the bounds of the circle.

map.fitBounds(circle.getBounds());

Jason Sanford

unread,
Apr 22, 2010, 8:46:48 PM4/22/10
to google-map...@googlegroups.com
Hi TND
What you could do is create a circle (google.maps.Circrle) with a radius of 20,25,50 miles with a transparent fill and stroke (unless you want them to see the circle) then tell the map to .fitBounds to the circle's .getBounds method like so:

var myLatLng = new google.maps.LatLng(35.265,-80.326);
var circleOptions = {
    center: myLatLng,
    fillOpacity: 0,
    strokeOpacity:0,
    map: myMapObject,
    radius: 32186 /* 20 miles */
}
var myCircle = new google.maps.Circle(circleOptions);
myMap.fitBounds(myCircle.getBounds());

I'm not sure that this code works as I just jotted it down but something like that should work. Maybe that will point you in the right direction.

Jason

Esa

unread,
Apr 24, 2010, 7:36:00 AM4/24/10
to Google Maps JavaScript API v3


On Apr 23, 3:46 am, Jason Sanford <jasonsanf...@gmail.com> wrote:
> Hi TND
> What you could do is create a circle (google.maps.Circrle) with a radius of
> 20,25,50 miles with a transparent fill and stroke (unless you want them to
> see the circle) then tell the map to .fitBounds to the circle's .getBounds

We have exactly the same idea! My posting just appeared first though
you have posted more than an hour before.

I would like to underline that you don't have to add the circle on map
if you don't like to. getBounds() is working if the circle is on map
or not.

To make sure I made a test page:
http://koti.mbnet.fi/ojalesa/v3/circleAndBounds.htm

Jason Sanford

unread,
Apr 25, 2010, 10:46:16 PM4/25/10
to google-map...@googlegroups.com
Good to know that Esa, Thanks!

The Net Duck

unread,
Apr 29, 2010, 8:15:50 PM4/29/10
to Google Maps JavaScript API v3
Wow thanks this was really helpful!
Reply all
Reply to author
Forward
0 new messages