Problem:
Google maps geocoder can return coordinates, accuracy and range for given location. However, how to display location on the map, you need to center the map and set the zoom level. My problem was to find right zoom level for given location.
I found one blog post that describes the solution but I wasn’t very happy with it.
After some investigations, measurements and calculations I came up with this formula:
zoom = 18-log2(3.3*range/sqrt(x*x+y*y))
where
log2 - base 2 logarithm
range - is returned by google geocoding service
x, y - measurements of the map in pixels
I haven’t tested it yet, so if you find it working or not, please let me know.
Update 4. 3. 2008: I tested it and it works perfectly!
This formula looks nice
But, can you please explain how you found it? In particular, I don’t know what the range parameter is. It doesn’t seem to be present in the doc.
I am using different url for geocoding.
http://maps.google.com.au/maps?q=Poprad,Slovakia&output=kml
If you check docs about kml
http://code.google.com/apis/kml/documentation/kml_tags_21.html#range
it describes range as a distance from the earth. Distance returned by google is bigger for big cities and smaller for smaller cities.
hi there,
Very Interesting investigation but i can’t make it work with javascript…
18-Math.log(3.3*4/Math.sqrt(398*398+256*256), Math.LOG2E)
if u have any ideas!
Cheers.
JavaScript has only natural (base E) logarithm.
logarithm base 2 can be rewritten as
log 2 (y) = Math.log(y)/Math.log(2)
where Math.log() is natural JavaScript logarithm.
So equation would look something like:
var zoom = 18-Math.log(3.3*range/Math.sqrt(x*x+y*y))/Math.log(2)
hey Dusan,
thanks for taking the time.
Cheers!
Hey Dusan, how can i use another url for geocoding?
Thank you!
Hi Dusan,
Thank’s for the formula, that inspire me to improve it.
check this out.
http://aiskahendra.blogspot.com/2009/01/set-zoom-level-of-google-map-base-on.html