我用下面的類從緯度/長x/y的轉換:
public static class StaticMapHelper
{
private const long offset = 268435456;
private const double radius = offset/Math.PI;
private static double LongitudeToX(double longitude)
{
return Math.Round(offset + radius * longitude * Math.PI/180);
}
private static double LatitudeToY(double latitude)
{
return Math.Round(offset - radius * Math.Log((1 + Math.Sin(latitude * Math.PI/180))/(1 - Math.Sin(latitude * Math.PI/180)))/2);
}
private static double XToLongitude(double x)
{
return ((Math.Round(x) - offset)/radius) * 180/Math.PI;
}
private static double YToLatitude(double y)
{
return (Math.PI/2 - 2 * Math.Atan(Math.Exp((Math.Round(y) - offset)/radius))) * 180/Math.PI;
}
public static GeoPoint XYToLongitudeLatitude(int offsetX, int offsetY, double centerLongitude, double centerLatitude, int googleZoom)
{
double zoom_factor = Math.Pow(2, 21 - googleZoom);
double longitude = XToLongitude(LongitudeToX(centerLongitude) + (offsetX * zoom_factor));
double latitude = YToLatitude(LatitudeToY(centerLatitude) + (offsetY * zoom_factor));
return new GeoPoint(longitude, latitude);
}
public static GeoPoint LongitudeLatitudeToXY(int offsetX, int offsetY, double centerLongitude, double centerLatitude, int googleZoom)
{
double zoom_factor = Math.Pow(2, 21 - googleZoom);
double x = (LongitudeToX(offsetX) - LongitudeToX(centerLongitude))/zoom_factor;
double y = (LatitudeToY(offsetY) - LatitudeToY(centerLatitude))/zoom_factor;
return new GeoPoint(x, y);
}
}