2

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?

Thanks!

TomBomb
  • 3,236
  • 5
  • 31
  • 40

2 Answers2

4

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.

golux
  • 43
  • 4
2

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));
javram
  • 2,635
  • 1
  • 13
  • 18
  • 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 Apr 04 '12 at 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 Apr 04 '12 at 02:57
  • Technically this is in PHP, but i'm trying to get an appropriate static google maps link like: "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 Apr 04 '12 at 03:12
  • Sorry, I am not much use when it comes to php. – javram Apr 04 '12 at 03:49
  • 1
    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 you – TomBomb Apr 13 '12 at 23:16
  • 1
    See question http://stackoverflow.com/questions/6048975/google-maps-v3-how-to-calculate-the-zoom-level-for-a-given-bounds - it seems to have an answer to how to do this manually. – Marcus Apr 24 '12 at 07:21