How to calculate appropriate google maps zoom level

Go To


I have a tricky question...I am using the google maps static API to create an image of a map with markers on it. What I am struggling to do is calculate the appropriate zoom level, given the positions of the two markers I want to display.

I have latitudeA, longitudeA, latitudeB, longitudeB, and the center point (easy). Does anyone know of a formula to solve for zoom level?


2012-04-04 01:00
by TomBomb


I took 3 static maps at levels 11, 12 and 13, and used the "Maps Labs" feature of Google Maps, holding down the shift key at the corresponding min/max latitude/longtitude marks. Then I wrote a Perl program to calculate the latitude/longtitude ranges, the expected values (with the formula I predicted by inspecting the data), and the deltas (ie. abs(expected - range)), which should be close to zero. Once I had convinced myself that I had gotten as close as I could, here were my results:

Z11:  LatRange[0.323999]  ExpLat[ 0.324]  DeltaLat[  0.0000000000000019]
Z11:  LonRange[0.439999]  ExpLon[  0.44]  DeltaLon[  0.0000000000000023]

Z12:  LatRange[0.161999]  ExpLat[ 0.162]  DeltaLat[  0.0000000000000010]
Z12:  LonRange[0.219999]  ExpLon[  0.22]  DeltaLon[  0.0000000000000011]

Z13:  LatRange[0.081000]  ExpLat[ 0.081]  DeltaLat[  0.0000000000000031]
Z13:  LonRange[0.109999]  ExpLon[  0.11]  DeltaLon[  0.0000000000000006]

And the formulas I used were:

ExpLat = 0.162 * (2 ** (12 - Z))
ExpLon = 0.220 * (2 ** (12 - Z))

Where ExpLat, ExpLon are the expected latitude and longtitude ranges, and Z is the zoom level.

2012-09-15 01:05
by golux
good answer golux - Jordan Richards 2012-09-16 18:27


Even though you are using the static maps api, you can still reference the regular maps script file, and then do a bounding box calculation:

map = new GMap2(document.getElementById("map"));

// Define the two corners of the bounding box
var sw = new GLatLng(59.0, 13.12); //any lat,lng pair
var ne = new GLatLng(60.35, 16.90);

var bounds = new GLatLngBounds(sw, ne);

var zoom = map.getBoundsZoomLevel(bounds));
2012-04-04 02:17
by javram
well technically I'm not using the maps api, all I'm trying to do is get a map image to embed in an email. Is there no way to do this (mathematically) without using GMap calls - TomBomb 2012-04-04 02:33
Are you using any type of programming language to calculate this, or are you just trying to copy/paste the link from somewhere - javram 2012-04-04 02:57
Technically this is in PHP, but i'm trying to get an appropriate static google maps link like:

",centerLng&zoom=zoomLevel&size=widthxheight&maptype=roadmap&markers=color:green%7Clabel:P%7CLat1,Long1&markers=color:red%7Clabel:D%7CLat2,Long2&sensor=false" - TomBomb 2012-04-04 03:12

Sorry, I am not much use when it comes to php - javram 2012-04-04 03:49
Sorry, i didn't do enough research. Apparently if you don't specify a zoom OR center, but include markers on the map, google static maps will fit the bounds for yo - TomBomb 2012-04-13 23:16
See question - it seems to have an answer to how to do this manually - Marcus 2012-04-24 07:21