ASP.NET WGS84坐标 转 GCJ02坐标 / BD09坐标
/*具体实现方法*/
namespace WGS84PositionConversion
{
public class CoordinateConverter
{
// PI值
private const double PI = 3.1415926535897932384626;
// 长半轴
private const double A = 6378245.0;
// 扁率
private const double EE = 0.00669342162296594323;
public static double transformLat(double x, double y)
{
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
+ 0.2 * Math.Sqrt(Math.Abs(x));
ret += (20.0 * Math.Sin(6.0 * x * PI) + 20.0 * Math.Sin(2.0 * x * PI)) * 2.0 / 3.0;
ret += (20.0 * Math.Sin(y * PI) + 40.0 * Math.Sin(y / 3.0 * PI)) * 2.0 / 3.0;
ret += (160.0 * Math.Sin(y / 12.0 * PI) + 320 * Math.Sin(y * PI / 30.0)) * 2.0 / 3.0;
return ret;
}
public static double transformLon(double x, double y)
{
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
* Math.Sqrt(Math.Abs(x));
ret += (20.0 * Math.Sin(6.0 * x * PI) + 20.0 * Math.Sin(2.0 * x * PI)) * 2.0 / 3.0;
ret += (20.0 * Math.Sin(x * PI) + 40.0 * Math.Sin(x / 3.0 * PI)) * 2.0 / 3.0;
ret += (150.0 * Math.Sin(x / 12.0 * PI) + 300.0 * Math.Sin(x / 30.0
* PI)) * 2.0 / 3.0;
return ret;
}
/***
* 判断是否在中国范围之内
*/
public static bool outOfChina(double lat, double lon)
{
if (lon < 72.004 || lon > 137.8347)
return true;
if (lat < 0.8293 || lat > 55.8271)
return true;
return false;
}
/***
* 把公式部分抽取出来
*/
public static void transform(double lat, double lon, out double mgLat, out double mgLon)
{
if (!outOfChina(lat, lon))
{
double dLat = transformLat(lon - 105.0, lat - 35.0);
double dLon = transformLon(lon - 105.0, lat - 35.0);
double radLat = lat / 180.0 * PI;
double magic = Math.Sin(radLat);
magic = 1 - EE * magic * magic;
double sqrtMagic = Math.Sqrt(magic);
dLat = (dLat * 180.0) / ((A * (1 - EE)) / (magic * sqrtMagic) * PI);
dLon = (dLon * 180.0) / (A / sqrtMagic * Math.Cos(radLat) * PI);
mgLat = lat + dLat;
mgLon = lon + dLon;
}
else
{
mgLat = lat; mgLon = lon;
}
}
/***
* wgs84到gcj02转换(高德、腾讯)
*/
public static void wgs84_To_Gcj02(double lat, double lon, out double mgLat, out double mgLon)
{
transform(lat, lon, out mgLat, out mgLon);
}
/***
* 百度坐标是在火星坐标基础上做的二次加密
*/
public static void gcj02_To_Bd09(double gg_lat, double gg_lon, out double BdLat, out double BdLon)
{
double lat, lng;
transform(gg_lat, gg_lon, out lat, out lng); // 转火星坐标
double xpi = PI * 3000.0 / 180.0;
double z = Math.Sqrt(lng * lng + lat * lat) + 0.00002 * Math.Sin(lat * xpi);
double theta = Math.Atan2(lat, lng) + 0.000003 * Math.Cos(lng * xpi);
BdLon = z * Math.Cos(theta) + 0.0065;
BdLat = z * Math.Sin(theta) + 0.006;
}
}
}
/*
函数调用
*/
static void Main(string[] args)
{
double wgs84Lon = 106.580211; // 这里替换为实际的WGS84经度值
double wgs84Lat = 29.671458; // 这里替换为实际的WGS84纬度值
double gcj02Lat, gcj02Lon;
CoordinateConverter.wgs84_To_Gcj02(wgs84Lat, wgs84Lon, out gcj02Lat, out gcj02Lon);
Console.WriteLine($"转换后的GCJ02坐标:{gcj02Lon.ToString("F11")},{gcj02Lat.ToString("F11")}");
double Bd09Lat, Bd09Lon;
CoordinateConverter.gcj02_To_Bd09(wgs84Lat, wgs84Lon, out Bd09Lat, out Bd09Lon);
Console.WriteLine($"转换后的BD09坐标:{Bd09Lon.ToString("F11")},{Bd09Lat.ToString("F11")}");
}
/* 结果展示 */