Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of getBoundsZoomLevel() in gmaps api 3 [duplicate]

In API v2, the map object had a handy method getBoundsZoomLevel(). I used it to get the zoom level which fits the bounds best, then manipulated this optimal zoom level somehow and finally set the desired zoom level.

I cannot find similar function in API v3. (What a continuous frustrating experience when moving from v2 to v3)

Do I really have to use map.fitBounds(), map.getZoom(), manipulate and setZoom() again? That's really stupid!

like image 898
Tomas Avatar asked Mar 23 '12 09:03

Tomas


People also ask

What are Google map bounds?

Class Bounds Represents an area in the cartesian plane.

How do I see the zoom level on Google Maps?

You can change the zoom level of the map using simple steps. Step 1 Go to Add or Edit Map page . Step 2 Select 'Default zoom level' in the 'Map Information section'. Step 3 click save map and see the changes.


1 Answers

Below is a function I have implemented:

/** * Returns the zoom level at which the given rectangular region fits in the map view.  * The zoom level is computed for the currently selected map type.  * @param {google.maps.Map} map * @param {google.maps.LatLngBounds} bounds  * @return {Number} zoom level **/ function getZoomByBounds( map, bounds ){   var MAX_ZOOM = map.mapTypes.get( map.getMapTypeId() ).maxZoom || 21 ;   var MIN_ZOOM = map.mapTypes.get( map.getMapTypeId() ).minZoom || 0 ;    var ne= map.getProjection().fromLatLngToPoint( bounds.getNorthEast() );   var sw= map.getProjection().fromLatLngToPoint( bounds.getSouthWest() );     var worldCoordWidth = Math.abs(ne.x-sw.x);   var worldCoordHeight = Math.abs(ne.y-sw.y);    //Fit padding in pixels    var FIT_PAD = 40;    for( var zoom = MAX_ZOOM; zoom >= MIN_ZOOM; --zoom ){        if( worldCoordWidth*(1<<zoom)+2*FIT_PAD < $(map.getDiv()).width() &&            worldCoordHeight*(1<<zoom)+2*FIT_PAD < $(map.getDiv()).height() )           return zoom;   }   return 0; } 
like image 133
Engineer Avatar answered Sep 19 '22 13:09

Engineer