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

1

23

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!

Parlin answered 23/3, 2012 at 9:49 Comment(4)
take a look at #6049475Loopy
Thanks @Dr.Molle. It's a pitty you must write your own function for that, so in this case I prefered the stupid solution I presented above. But I will use your link for another problem, thanks!!Parlin
+1 - I'm surprised you haven't received more up-votes.Posterior
what are you missing from map.fitBounds()? so far that seems to do a pretty good job centering the map on the bounds, and going to a zoom level thats fitting for the map size and bounds sizeCorregidor
N
37

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;
}
Neper answered 2/4, 2012 at 18:56 Comment(7)
Thank you! Your code produces the same values as the old v2 getZoomByBoundsEmployee
One can use (map.getDiv()).style.width instead of $(map.getDiv()).width(), same as height also.Hautemarne
@Hautemarne I think you mean .offsetWidth and .offsetHeight (the style attributes may not be set)Milburt
@Milburt Yes buddy, because sometimes $(map.getDiv()).width() returns undefined so one can go with (map.getDiv()).style.width also.Hautemarne
I wondered it wasn't working in first place, but then I figured out it needed a "projection_changed"-Event Listener. Now it works like a charm. Just mentioning it if anyone else has the same problem.Meingolda
I have been looking for this for over 3 hours. I even wrote a checker that would check random bounds and this function works out of the 20,000 random checks I've done. Seems to always match what google says the bounded zoom will be.Zolazoldi
How can I give it padding in only one direction? lets say padding from bottom of mapReitareiter

© 2022 - 2024 — McMap. All rights reserved.