Set map bounds in order to be contained inside a given box

40 views
Skip to first unread message

InfoMail InfoMail

unread,
Jun 24, 2011, 10:43:02 AM6/24/11
to google-map...@googlegroups.com
I'd like to set map bounds (i.e. a setZoom + setCenter) in order to be included in a given box.
Let's have a fixed center,
 
function includeBounds(minLat, minLng, maxLat, maxLng) {
 
    var center = new google.maps.LatLng((minLat + maxLat) / 2, (minLng + maxLng) / 2);
    var zoom = someMagicFunction(center, minLat, minLng, maxLat, maxLng);
 
    map.setCenter(center);
    map.setZoom(zoom);
   
    at this point I'd like to have the map centered in 'center' and with a zoom level such
    that map.getBounds() are INCLUDED in the box defined by [minLat, minLng] - [maxLat, maxLng]
}
 
Any idea?
Many thanks in advance!

Jeremy Geerdes

unread,
Jun 24, 2011, 10:46:04 AM6/24/11
to google-map...@googlegroups.com
Check out the google.maps.LatLngBounds object and the google.maps.Map object's fitBounds method.

Jeremy R. Geerdes
Generally Cool Guy
Des Moines, IA

For more information or a project quote:

If you're in the Des Moines, IA, area, check out Debra Heights Wesleyan Church!

--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-maps-js-api-v3/-/ibpzPZ0_7xwJ.
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.

Rossko

unread,
Jun 24, 2011, 12:30:34 PM6/24/11
to Google Maps JavaScript API v3
> zoom level such
>     that map.getBounds() are INCLUDED in the box defined by [minLat, minLng]
> - [maxLat, maxLng]

I'm not sure I'm understanding you right, but I think you may mean you
want the view to be INSIDE your given bounds? Not the standard
fitBounds which gives a view to INCLUDE your bounds.
In which case use fitBounds and then just zoom in by 1 ?
Reply all
Reply to author
Forward
0 new messages