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?
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.
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));
"http://maps.googleapis.com/maps/api/staticmap?center=centerLat,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