根据高德地图js代码翻译而成,废话不多说直接上代码
const double VV = 0.15915494309189535;
const double kW = 0.5;
const double yW = -0.15915494309189535;
const double WW = 0.5;
/// <summary>
/// 高德地图坐标转平面坐标
/// </summary>
/// <param name="lng">经度</param>
/// <param name="lat">纬度</param>
/// <param name="zoom">缩放级别</param>
/// <returns></returns>
public static (double, double) LnglatToPixel(double lng, double lat, int zoom = 13)
{
double b = Math.PI / 180;
double c = 85.0511287798;
c = Math.Max(Math.Min(c, lat), -c);
double x = lng * b;
double y = Math.Log(Math.Tan(Math.PI / 4 + c * b / 2));
double scale = 256 << zoom;
return (scale * (VV * x + kW), scale * (yW * y + WW));
}
/// <summary>
/// 平面坐标转高德坐标
/// </summary>
/// <param name="x">x坐标</param>
/// <param name="y">y坐标</param>
/// <param name="zoom">缩放级别</param>
public static (double, double) PixelToLnglat(double x, double y, int zoom = 13)
{
double scale = 256 << zoom;
double a = (x / scale - kW) / VV;
double b = (y / scale - WW) / yW;
double c = 180 / Math.PI;
return (a * c, (2 * Math.Atan(Math.Exp(b)) - Math.PI / 2) * c);
}
效果演示
效果完美!!
网友评论