首页 > 其他分享 >墨卡托坐标转换(经纬度与米之间互转)

墨卡托坐标转换(经纬度与米之间互转)

时间:2022-12-25 17:55:27浏览次数:42  
标签:经纬度 double 墨卡托 var 180 yValue 互转 PI Math

https://blog.csdn.net/a_dev/article/details/80990492

 

//要加入GeoJSON

//调用 示例

  var s = MercatorCoordinatesConverter.WebMercatorMeter2Degree(12582978.277218819, 2576021.646897778);

 

public class MercatorCoordinatesConverter
{
/// <summary>
/// 经纬度转Web墨卡托(单位:米)
/// </summary>
/// <param name="longitude">经度</param>
/// <param name="latitude">纬度</param>
/// <returns>转换后的位置</returns>
public static Position Degree2WebMercatorMeter(double longitude, double latitude)
{
var xValue = longitude * 20037508.34 / 180;
var y = Math.Log(Math.Tan((90 + latitude) * Math.PI / 360)) / (Math.PI / 180);
var yValue = y * 20037508.34 / 180;
return new Position(xValue, yValue);
}

/// <summary>
/// 经纬度转World墨卡托(单位:米)
/// </summary>
/// <param name="longitude">经度</param>
/// <param name="latitude">纬度</param>
/// <returns>转换后的位置</returns>
public static Position Degree2WorldMercatorMeter(double longitude, double latitude)
{
const int radius = 6378137;
const double minorRadius = 6356752.314245179;

const double d = Math.PI / 180;
const double r = radius;
var y = latitude * d;
const double tmp = minorRadius / r;
double e = Math.Sqrt(1 - tmp * tmp),
con = e * Math.Sin(y);

var ts = Math.Tan(Math.PI / 4 - y / 2) / Math.Pow((1 - con) / (1 + con), e / 2);
y = -r * Math.Log(Math.Max(ts, 1E-10));

var xValue = longitude * d * r;
var yValue = y;

return new Position(xValue, yValue);
}

/// <summary>
/// Web墨卡托转经纬度
/// </summary>
/// <param name="x">X坐标值(单位:米)</param>
/// <param name="y">Y坐标值(单位:米)</param>
/// <returns>转换后的位置</returns>
public static Position WebMercatorMeter2Degree(double x, double y)
{
var xValue = x / 20037508.34 * 180;
var yValue = y / 20037508.34 * 180;
yValue = 180 / Math.PI * (2 * Math.Atan(Math.Exp(yValue * Math.PI / 180)) - Math.PI / 2);
var longitude = xValue;
var latitude = yValue;
return new Position(longitude, latitude);
}
}

标签:经纬度,double,墨卡托,var,180,yValue,互转,PI,Math
From: https://www.cnblogs.com/evemen/p/17004317.html

相关文章