auto center & auto zoom

3,527 views
Skip to first unread message

knospe

unread,
Apr 10, 2011, 4:55:11 PM4/10/11
to Google Maps JavaScript API v3
What do I have to do to auto-fill the zoom and the center according to
the coords of the polygon?

How do I get

"var myLatLng = new google.maps.LatLng(24.886436490787712,
-70.2685546875);"
and
"zoom: 5,"
automatically?

google's example code:

function initialize() {
var myLatLng = new google.maps.LatLng(24.886436490787712,
-70.2685546875);
var myOptions = {
zoom: 5,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.TERRAIN
};

var bermudaTriangle;

var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);

var triangleCoords = [
new google.maps.LatLng(25.774252, -80.190262),
new google.maps.LatLng(18.466465, -66.118292),
new google.maps.LatLng(32.321384, -64.75737),
new google.maps.LatLng(25.774252, -80.190262)
];

// Construct the polygon
// Note that we don't specify an array or arrays, but instead just
// a simple array of LatLngs in the paths property
bermudaTriangle = new google.maps.Polygon({
paths: triangleCoords,
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#FF0000",
fillOpacity: 0.35
});

bermudaTriangle.setMap(map);
}

geoco...@gmail.com

unread,
Apr 10, 2011, 6:12:06 PM4/10/11
to Google Maps JavaScript API v3
On Apr 10, 1:55 pm, knospe <kno...@gmail.com> wrote:
> What do I have to do to auto-fill the zoom and the center according to
> the coords of the polygon?
>

The most general way:
1. create google.maps.LatLngBounds object that contains the bounds of
the polygon.
2. call map.fitBounds with that bounds object as its argument.

If you actually need the center and zoom level being used, you can get
that by calling map.getCenter() and map.getZoom() after the bounds has
taken effect.

-- Larry

geoco...@gmail.com

unread,
Apr 11, 2011, 7:58:24 AM4/11/11
to Google Maps JavaScript API v3
On Apr 10, 3:12 pm, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:
Here is an example (probably not the simplest):
http://www.geocodezip.com/v3_polygon_example_clickable_squares.html
(click on the links in the table on the side to center and zoom on the
specified square)

  -- Larry

sb

unread,
Apr 11, 2011, 10:53:08 PM4/11/11
to Google Maps JavaScript API v3
var myBounds = new google.maps.LatLngBounds();
myBounds.extend(25.774252, -80.190262);
myBounds.extend(18.466465, -66.118292);
myBounds.extend(L32.321384, -64.7573);
map.fitBounds(myBounds);

This should work.

sb

unread,
Apr 12, 2011, 3:59:14 PM4/12/11
to Google Maps JavaScript API v3
My mistake. The points will need to loaded as points, not as numbers.
I have set up a simple example;
http://srsz750.appspot.com/api3/fitbounds.html

If you change the size of the window and refresh the map you will
notice that map will change so all points are in the display area.
Reply all
Reply to author
Forward
0 new messages