Placing pushpin on a tile?

I have following problem…

Lets say I know long/lat of my town. I know how to get mapnik tile for my town on some zoom level. Now…how can I place pushpin on the same tile on lets say my own house long/lat? I need to know exact transformation for long/lat of my house to tile’s pixels? Is there any formula?

In short…how can I find X and Y (in pixels) on the tile using lat/long (in the world)?

Application Im developing is not web application. Im downloading tiles and manipulate with them. Im using .net c#.

http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#lon.2Flat_to_tile_numbers

When you use this code to get the tile number you are actually doing a projection (transformation) from a lat/lon projection to spherical-mercator flat projection. So you have all the math… :slight_smile:

Anyways here you have some python code that does excatly what you want, if you manage to translate something of this into C# then please post it.

http://svn.openstreetmap.org/applications/routing/pyroute/projection.py
http://svn.openstreetmap.org/applications/routing/pyroute/tiles.py
http://svn.openstreetmap.org/applications/routing/pyroute/tilenames.py

I am currently struggling with the same problem. I am working on a robotics control system where one or more robots progress are tracked and displayed on a map. The OpenStreetMap tile system is perfect for this. I can successfully download the tiles stitch them together, scroll, zoom etc.

I am however having difficulty finding the pixel in a tile which represents a specific lat/lon.

I currently have this which I have ported from tangoGPS (www.tangogps.org).

    public double ConvertDegreesToRadians(double degrees)
    {
        double radians = (Math.PI / 180) * degrees;
        return (radians);
    }

    public static double atanh(double x)
    {
        if (x > 1.0 || x < -1.0) 
            throw new ArithmeticException("range exception");

        return 0.5 * Math.Log((1.0 + x) / (1.0 - x));
    }

    public int LatToPixel(int ZoomLevel, double Lat)
    {
        double latm = atanh(Math.Sin(lat)); 
        return (int)Math.Truncate(-((latm * 256 * Math.Exp(ZoomLevel * Math.Log(2))) / (2 * Math.PI)) + 
                                   (Math.Exp(ZoomLevel * Math.Log(2)) * (256 / 2))); 
    }

    public int LonToPixel(int ZoomLevel, double Lon)
    {
        return (int)Math.Truncate(((Lon * 256 * Math.Exp(ZoomLevel * Math.Log(2))) / (2 * Math.PI)) + 
                                  (Math.Exp(ZoomLevel * Math.Log(2)) * (256 / 2))); 
    }

int xpixel = LonToPixel(zoomlevel, ConvertDegreesToRadians(poilon));
int ypixel = LatToPixel(zoomlevel, ConvertDegreesToRadians(poilat));

The Lon values seems ok, however the Lat does not. Did I make a mistake in the porting? Does anyone know of a diffirent source I can look at or some documentation on the formulas?

Adi