Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting lon/lat from pixel coords in Google Static Map

I have a JAVA project to do using Google Static Maps and after hours and hours working, I can't get a thing working, I will explain everything and I hope someone will be able to help me.

I am using a static map (480pixels x 480pixels), the map's center is lat=47, lon=1.5 and the zoom level is 5.

Now what I need is being able to get lat and lon when I click a pixel on this static map. After some searches, I found that I should use Mercator Projection (right ?), I also found that each zoom level doubles the precision in both horizontal and vertical dimensions but I can't find the right formula to link pixel, zoom level and lat/lon...

My problem is only about getting lat/lon from pixel, knowing the center's coords and pixel and the zoom level...

Thank you in advance !

like image 803
user1374021 Avatar asked May 04 '12 02:05

user1374021


2 Answers

Use the Mercator projection.

If you project into a space of [0, 256) by [0,256]:

LatLng(47,=1.5) is Point(129.06666666666666, 90.04191318303863)

At zoom level 5, these equate to pixel coordinates:

x = 129.06666666666666 * 2^5 = 4130
y = 90.04191318303863 * 2^5 = 2881

Therefore, the top left of your map is at:

x = 4130 - 480/2 = 4070
y = 2881 - 480/2 = 2641

4070 / 2^5 = 127.1875
2641 / 2^5 = 82.53125

Finally:

Point(127.1875, 82.53125) is LatLng(53.72271667491848, -1.142578125)
like image 57
Chris Broadfoot Avatar answered Sep 18 '22 11:09

Chris Broadfoot


Google-maps uses tiles for the map to efficient divide the world into a grid of 256^21 pixel tiles. Basically the world is made of 4 tiles in the lowest zoom. When you start to zoom you get 16 tiles and then 64 tiles and then 256 tiles. It basically a quadtree. Because such a 1d structure can only flatten a 2d you also need a mercantor projection or a conversion to WGS 84. Here is a good resource Convert long/lat to pixel x/y on a given picture. There is function in Google Maps that convert from lat-long pair to pixel. Here is a link but it says the tiles are 128x128 only: http://michal.guerquin.com/googlemaps.html.

  1. Google Maps V3 - How to calculate the zoom level for a given bounds
  2. http://www.physicsforums.com/showthread.php?t=455491
like image 24
Micromega Avatar answered Sep 22 '22 11:09

Micromega