php - Google static map radius to zoom -


i'm developing web service in php gets latitude, longitude , radius in kilometers , generate google static map.

also display points database in radius.

my question how can covert radius in google's zoom parameter ensure points visible on map?

something like: http://maps.googleapis.com/maps/api/staticmap?center=new+york,ny&[*this parameter*zoom=13]&size=600x300&markers.......

as stated @ google maps documentation:

because basic mercator google maps tile 256 x 256 pixels.
note every zoom level, map has 2 n tiles.

meaning @ zoomlevel 2, pixels in direction of map = 256 * 2² = 1024px.

taking account earth has perimeter of ~40,000 kilometers, in zoom 0, every pixel ~= 40,000 km/256 = 156.25 km
@ zoom 9, pixels 131072:
1px = 40,000 km / 131072 = 0.305 km ... , on.

if image 640 x 640 px,and using zoom 9, means cover ~ 640 * 0.305 ~= 195km. radius approx 100km.
everytime zoom++, radius half.

if use zoom 15 (1px = 0.00478km) image cover ~3km wide (radius 1.5km).

take account go further north or south, real distance less, distortion bigger.

you use parameter zoom = floor(log2 (earthp * 2 pixmap / (256 *radius))).

  • earthp = earth perimeter @ latitude (in km)
  • pixmap = pixels of map display
  • radius = radius want displayed in image (in km)
  • log 2= logarithm in base 2

note: convert log2 regular log make this:
floor((log (earthp * 2 pixmap / (256 *radius)))/log 2)

reference logarithms in wikipedia


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -