I spent two nights writing a google map application, using Map api, weather api, and localsearch api. I want to write some articles about Google map API, today, I will write a method for automatically scaling a map to an appropriate level. Here, auto scaling refers to the fact that many Gmarker points have been added to the map, which are available anywhere, in order for them to be displayed on the Map, a Google Map must be used to set a proper zoom level and center point. The method used here is the GLatLngBounds of the Google Map API.
Assume that the Google Map object we created is gmap = new GMap2 (). We add all GMarker stored in an array named markers, and their points are everywhere, all we need to do is to display these points on the map, that is, the automatically scaled map (set auto zoom ).
First we bounds = new GLatLngBounds (); then we use a loop to pass all GLatLng objects in GMarker to bounds, that is, bounds. extend (markers [I]. getLatLng (). After completing the loop, we can use the following code set auto zoom:
Gmap. setCenter (bounds. getCenter (), gmap. getBoundsZoomLevel (bounds );
The following setAutoZoom function is used in combination:
Function setAutoZoom (gmap, markers ){
Var I = markers. length, bounds = new GLatLngBounds ();
While (I --){
Bounds. extend (new GLatLng (markers [I]. y, markers [I]. x ));
}
Gmap. setCenter (bounds. getCenter (), gmap. getBoundsZoomLevel (bounds ));
}
View:Google Map Api setAutoZoom instance