Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A similar question has been asked on the Google group: <a href="http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/e6448fc197c3c892">http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/e6448fc197c3c892</a></p> <p>The zoom levels are discrete, with the scale doubling in each step. So in general you cannot fit the bounds you want exactly (unless you are very lucky with the particular map size).</p> <p>Another issue is the ratio between side lengths e.g. you cannot fit the bounds exactly to a thin rectangle inside a square map.</p> <p>There's no easy answer for how to fit exact bounds, because even if you are willing to change the size of the map div, you have to choose which size and corresponding zoom level you change to (roughly speaking, do you make it larger or smaller than it currently is?).</p> <p>If you really need to calculate the zoom, rather than store it, this should do the trick:</p> <p>The Mercator projection warps latitude, but any difference in longitude always represents the same fraction of the width of the map (the angle difference in degrees / 360). At zoom zero, the whole world map is 256x256 pixels, and zooming each level doubles both width and height. So after a little algebra we can calculate the zoom as follows, provided we know the map's width in pixels. Note that because longitude wraps around, we have to make sure the angle is positive.</p> <pre><code>var GLOBE_WIDTH = 256; // a constant in Google's map projection var west = sw.lng(); var east = ne.lng(); var angle = east - west; if (angle &lt; 0) { angle += 360; } var zoom = Math.round(Math.log(pixelWidth * 360 / angle / GLOBE_WIDTH) / Math.LN2); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload