[OSM-dev] How to translate coordinate to pixel for a tile ?
Dave Stubbs
osm.list at randomjunk.co.uk
Thu Nov 15 13:29:18 GMT 2007
On 15/11/2007, Rune Baggetun <baggetun at gmail.com> wrote:
> Hi
> I am currently experimenting with OSM and incorporating it into a J2ME
> ('java for mobiles') application . Using example code from the wiki
> [1] I can retrieve individual tiles based on latitude- longitude data,
> that I get over bluetooth from an external GPS device, into my
> applications, but how do I translate between coordinates and pixels in
> the retrieved tile ? For example If I want to draw a marker denoting
> the position I get from my GPS device on the top of the tile I
> retrieve. Is there an API call for this?
>
> [1 ]http://wiki.openstreetmap.org/index.php/Slippy_map_tilenames
where it does "* 2**z" (or "1<<zoom") just multiply by an extra 256
(the pixel size of a tile).
then:
tile number = floor(n/256)
pixel = n mod 256
so:
my $x = ($lon+180)/360 *2**$z * 256;
my $y = (1 - log(tan($lat*pi/180) + sec($lat*pi/180))/pi)/2 *2**$z * 256;
my $xtile = int($x/256);
my $ytile = int($y/256);
my $xpixel = $x % 256;
my $ypixel = $y % 256;
More information about the dev
mailing list