Convert pixel position to latlng.

3,502 views
Skip to first unread message

Kesuke

unread,
May 2, 2011, 11:48:25 AM5/2/11
to Google Maps JavaScript API v3
How do I convert a pixel position to a latlng. I've searched and
searched but its suprisingly hard to get a clear answer.

For example, at zoom level 2 lets say I want to get the latlng of a
position that is 40% along the x axis and 60% along the y-axis.

So;

2^zoomlevel * 256 = 1024 pixel map.
x co-ordinate = 1024 * 0.4 = 410
y co-ordinate = 1024 * 0.6 = 614

How can i now convert those pixel co-ordinates (410,614) into a
corresponding latlng?

I am using the bog-standard mercator projection.

Kesuke

unread,
May 5, 2011, 5:54:52 PM5/5/11
to Google Maps JavaScript API v3
Hate to bump, but someone must have done this as it's been asked here
a lot before (just with no clear answer). I'm sure one of us has the
maths for this ready to go.

Kesuke

unread,
May 5, 2011, 7:17:40 PM5/5/11
to Google Maps JavaScript API v3
I *think* i have made a tiny bit of progress but I'm still stuck. It
seems that in a mercator projection the longtitude (x axis) scales
normally. So because I am trying to get a % pixel position on the map,
I can simply say "(percentage position * 360) - 180" to get the
longtitude.

HOWEVER, latitude (y-axis) does not scale so simply, gridlines have a
scaling factor of 1 at 0 degrees but spread out towards infinity at
the poles. What I need is a bit of maths that I can use to convert a %
or pixel position into a latitude. I have a suspiscion what I need
might be called a reverse gudermannian function but it is at this
point my knowledge and understanding of maths is clutching at straws.

Does anyone have such a function that I can copy paste into my code to
convert a % latitute position on a square map into a trye latitude
value?
> > I am using the bog-standard mercator projection.- Hide quoted text -
>
> - Show quoted text -

Enoch Lau

unread,
May 6, 2011, 12:07:49 AM5/6/11
to google-map...@googlegroups.com
Hi Kesuke

Have you taken a look at the Projection object? You can get one by calling getProjection() on your map.

Enoch

Martin™

unread,
May 6, 2011, 12:12:27 AM5/6/11
to Google Maps JavaScript API v3
There's some info on coordinates here:

http://code.google.com/apis/maps/documentation/javascript/maptypes.html#CustomMapTypes

Martin.

On May 6, 5:07 am, Enoch Lau <enoch...@google.com> wrote:
> Hi Kesuke
>
> Have you taken a look at the Projection<http://code.google.com/apis/maps/documentation/javascript/reference.h...>object? You can get one by calling
> getProjection()<http://code.google.com/apis/maps/documentation/javascript/reference.h...>on your map.
>
> Enoch

Kesuke

unread,
May 6, 2011, 9:00:39 AM5/6/11
to Google Maps JavaScript API v3
Thanks all, got it working. I had originally tried the
fromLatLngToPoint(point) method but it didn't work so I moved on.
HOWEVER, it turns out the problem was I was not feeding it points
properly. I was trying to give it pixel coords or tile coords - what
it actually wants are world coords! Like many problems it evolved from
me not understanding the difference between the different coordinate
systems used by the API. So for anyone searching:

Pixel coords = 2^zoomlevel * tile size. This is the true pixel
position on the map. So the center position on a 1024 pixel (e.g. zoom
level 2) map would be (512,512). Origin is at the top left and it is
given as (x,y).
World coords = a value between 0 and 256 for each of x and y. So
basically the pixel position at zoom level 0. So the center position
will be (256,256). Origin is also at the top left and is given as
(x,y).
Tile coords = Math.floor(pixel coords / tile size). These are tile
sized square divisions of the map, and best not used for most purposes
I imagine since you lose the position within the tile. Again the
origin is top left and written as (x,y).
Latitude/Longtitude coords = this will depend on projection, but in a
mercator projection longtidude is: (relative position / 360) - 180 (as
the origin is in the center rather the top left as it is with the
other 3 methods). Latitude is much more complicated, look at mercator
projection on wikipedia for the gudermannian equation. Significantly,
latitude breaks from normal coordinate convention and so its written
as (lat, long).

The problem with the docmentation is its very clear in the tutorial
section: http://code.google.com/apis/maps/documentation/javascript/maptypes.html#CustomMapTypes
but it isn't at all clear in the API reference section. For example in
the .fromPointToLatLng() method it describes it using the term 'pixel'
- so I assumed it wanted to be fed pixel coordinates, when infact it
wants world coordinates. Stupid mistake on my part but its cost me
about 5 hours of my life.
Reply all
Reply to author
Forward
0 new messages