Alex Rivera | Logout

Getting lon/lat from pixel coords in Google Static Map

Asked 2012-05-04T02:54:40.617
9

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 !

Edit
Report

1 Answer

2

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)
answered 2012-05-04T20:45:27.733

Your Answer