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