Help with world coordinate to pixel location on tile

551 views
Skip to first unread message

Ben

unread,
Jun 12, 2011, 5:42:40 PM6/12/11
to Google Maps JavaScript API v3
Hi,

I am having trouble going from the 'world coordinate' to the pixel
location on the tile. From my understanding of the docs the x and y
value from the world coordinate are mapped to the tile's x and y
pixel? When I add a pixel to that location on the tile, it is off from
the latlng location. ( the marker location ) I based my testing on the
example from the link below. The world coordinates seem correct.

http://code.google.com/apis/maps/documentation/javascript/examples/map-coordinates.html

The one thing I do notice is that the world coordinate can't be an
exact xy of the tile since when i change the maptype to base, it isn't
at (65,95) on the tile.

Any idea what my issue might be?

Thank you in advance.

Martin™

unread,
Jun 13, 2011, 4:14:46 AM6/13/11
to Google Maps JavaScript API v3
Hi.

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

World Coordinates range from 0 to 256 for both x and y values.
At zoom level 0 those world coordinates are the same as the pixel
coordinets that you want.

At higher zoom levels simply take each x and y value and multiply by 2
to the power of the zoom level.

Martin.


On Jun 12, 10:42 pm, Ben <xb3...@gmail.com> wrote:
> Hi,
>
> I am having trouble going from the 'world coordinate' to the pixel
> location on the tile. From my understanding of the docs the x and y
> value from the world coordinate are mapped to the tile's x and y
> pixel? When I add a pixel to that location on the tile, it is off from
> the latlng location. ( the marker location ) I based my testing on the
> example from the link below. The world coordinates seem correct.
>
> http://code.google.com/apis/maps/documentation/javascript/examples/ma...

Ben

unread,
Jun 13, 2011, 7:42:16 AM6/13/11
to Google Maps JavaScript API v3
Martin,

That is the part that I am not understanding. I am getting the right
tile.

Every tile is 256 x 256

So.. at zoom 1 it would be x * 2 ^ z. What I know is this gives me the
pixel notation but if the tile is 256 pixels at all zooms, what is
this for?


Example:

x = 5;
y= 5;

At zoom 0, pixel coord is x = 5, y = 5.

At zoom 10, pixel coord is x = 19531250, y = 19531250.

If there is only 256 x 256 pixels what are these values for?

Thanks for your help.


On Jun 13, 4:14 am, Martin™ <warwo...@gmail.com> wrote:
> Hi.
>
> http://code.google.com/apis/maps/documentation/javascript/maptypes.ht...

Jeremy Geerdes

unread,
Jun 13, 2011, 8:37:25 AM6/13/11
to google-map...@googlegroups.com
As the zoom level goes up, the map consists of more tiles. At zoom level 10, the world is a box 1024 tiles square (i.e., 1024 x 1024), covering a square 262144 pixels on a side. So, to find the pixel coordinate of point given in world coordinates, take [world_coordinate] * (2 ^ zoom). To find the tile that your point is supposed to be on, find the integer value of [x_pixel_coordinate] / 256 and [y_pixel_coordinate] / 256. And to find the coordinate on the tile, find the remainder of the same. A point at world coords (5, 5) should be at pixel coords (5120, 5120) at zoom level 10. This would be on tile (20, 20). And on the tile, it would be at coordinates (0, 0).

The challenge with this is that, as the zoom level increases, the fidelity of your point must also increase. The one pixel point at zoom 0 represents a block 1024 x 1024 pixels at zoom 10, completely covering 16 entire tiles (4 x 4 tiles). By the time you get to zoom 17, it's 131072 pixels square, covering 262144 tiles (512 x 512 tiles) total. This is why the world coordinates given on the example are precise to 14 decimals. So you can't just assume that the pixel coordinates at zoom level 0 are equivalent to the world coordinates.

Jeremy R. Geerdes
Generally Cool Guy
Des Moines, IA

For more information or a project quote:
jrge...@gmail.com

If you're in the Des Moines, IA, area, check out Debra Heights Wesleyan Church!

> --
> You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
> To post to this group, send email to google-map...@googlegroups.com.
> To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.
>

Ben

unread,
Jun 13, 2011, 9:14:06 PM6/13/11
to Google Maps JavaScript API v3
Jeremy,

After rereading and rereading I got it. The line "And to find the
coordinate on the tile, find the remainder of the same.", took me out
of tunnel vision, and let me see the light. The pixel now lines up
perfectly. Thank you kind sir. Now only if I could get my Saturday
back...

Ben



On Jun 13, 8:37 am, Jeremy Geerdes <jrgeer...@gmail.com> wrote:
> As the zoom level goes up, the map consists of more tiles. At zoom level 10, the world is a box 1024 tiles square (i.e., 1024 x 1024), covering a square 262144 pixels on a side. So, to find the pixel coordinate of point given in world coordinates, take [world_coordinate] * (2 ^ zoom). To find the tile that your point is supposed to be on, find the integer value of [x_pixel_coordinate] / 256 and [y_pixel_coordinate] / 256. And to find the coordinate on the tile, find the remainder of the same. A point at world coords (5, 5) should be at pixel coords (5120, 5120) at zoom level 10. This would be on tile (20, 20). And on the tile, it would be at coordinates (0, 0).
>
> The challenge with this is that, as the zoom level increases, the fidelity of your point must also increase. The one pixel point at zoom 0 represents a block 1024 x 1024 pixels at zoom 10, completely covering 16 entire tiles (4 x 4 tiles). By the time you get to zoom 17, it's 131072 pixels square, covering 262144 tiles (512 x 512 tiles) total. This is why the world coordinates given on the example are precise to 14 decimals. So you can't just assume that the pixel coordinates at zoom level 0 are equivalent to the world coordinates.
>
> Jeremy R. Geerdes
> Generally Cool Guy
> Des Moines, IA
>
> For more information or a project quote:
> jrgeer...@gmail.com
Reply all
Reply to author
Forward
0 new messages