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);
    }
    /***
     * gcj02到wgs84转换
     */
    public static void gcj02_To_Wgs84_exact(double lat, double lon, out double wgsLat, out double wgsLon)
    {
        if (!outOfChina(lat, lon))
        {
            double initDelta = 0.01;
            double threshold = 0.000001;
            double dLat = initDelta;
            double dLon = initDelta;
            double mLat = lat - dLat;
            double mLon = lon - dLon;
            double pLat = lat + dLat;
            double pLon = lon + dLon;
            int i = 0;
            double dLat_new = 0, dLon_new = 0;
            while (true)
            {
                wgsLat = (mLat + pLat) / 2;
                wgsLon = (mLon + pLon) / 2;
                wgs84_To_Gcj02(wgsLat, wgsLon, out dLat_new, out dLon_new);
                dLat = dLat_new - lat;
                dLon = dLon_new - lon;
                if ((Math.Abs(dLat) < threshold) && (Math.Abs(dLon) < threshold))
                {
                    break;
                }
                if (dLat > 0) { pLat = wgsLat; } else { mLat = wgsLat; }
                if (dLon > 0) { pLon = wgsLon; } else { mLon = wgsLon; }
                if (++i > 1000) break;
            }
        }
        else
        {
            wgsLat = lat; wgsLon = lon;
        }
    }
    /***
     * 百度坐标是在火星坐标基础上做的二次加密
     */
    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")}");
    double wgs84Lat_new, wgs84Lon_new;
    CoordinateConverter.gcj02_To_Wgs84_exact(gcj02Lat, gcj02Lon, out wgs84Lat_new, out wgs84Lon_new);
    Console.WriteLine($"转换后的wgs84Lon坐标:{wgs84Lon_new.ToString("F6")},{wgs84Lat_new.ToString("F6")}");
}
/* 结果展示 */

                    
                
                
            
        
浙公网安备 33010602011771号