常用GPS坐标系转换类

class GPSPosition
    {
        public double Latitude { get; set; }
        public double Longitude { get; set; }
    }

    /// 
    /// http://www.oschina.net/code/snippet_260395_39205
    /// 
    class GPSConverter
    {
        static readonly double PI = Math.PI;
        static readonly double x_pi = Math.PI * 3000.0 / 180.0;
        static readonly double a = 6378245.0; //卫星椭球坐标投影到平面地图坐标系的投影因子。
        static readonly double ee = 0.00669342162296594323;//椭球的偏心率。

        /// 
        /// 中国坐标偏移标准,Google Map、高德、腾讯使用 to 百度坐标偏移标准,Baidu Map使用
        /// 
        /// 
        /// 
        /// 
        public static GPSPosition GCJ02ToBD09(double lng, double lat)
        {
            double x = lng, y = lat;
            var z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * x_pi);
            var theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * x_pi);
            var bdLon = z * Math.Cos(theta) + 0.0065;
            var bdLat = z * Math.Sin(theta) + 0.006;
            return new GPSPosition()
            {
                Latitude = bdLat,
                Longitude = bdLon
            };
        }

        /// 
        /// 中国坐标偏移标准,Google Map、高德、腾讯使用 to 国际标准,GPS坐标(Google Earth使用、或者GPS模块)
        /// 
        /// 
        /// 
        /// 
        public static GPSPosition GCJ02ToWGS84(double lng, double lat)
        {
            var initDelta = 0.01;
            var threshold = 0.000000001;
            double dLat = initDelta, dLon = initDelta;
            double mLat = lat - dLat, mLon = lng - dLon;
            double pLat = lat + dLat, pLon = lng + dLon;
            double wgsLat, wgsLon, i = 0;
            while (true)
            {
                wgsLat = (mLat + pLat) / 2;
                wgsLon = (mLon + pLon) / 2;
                var tmp = gcj_encrypt(wgsLat, wgsLon);
                dLat = tmp.lat - lat;
                dLon = tmp.lon - lng;
                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 > 10000) break;
            }
            //console.log(i);
            return new GPSPosition()
            {
                Latitude = wgsLat,
                Longitude = wgsLon
            };
        }

        /// 
        /// 百度坐标偏移标准,Baidu Map使用 to 中国坐标偏移标准,Google Map、高德、腾讯使用
        /// 
        /// 
        /// 
        /// 
        public static GPSPosition BD09ToGCJ02(double bdLon, double bdLat)
        {
            double x = bdLon - 0.0065, y = bdLat - 0.006;
            var z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * x_pi);
            var theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * x_pi);
            var gcjLon = z * Math.Cos(theta);
            var gcjLat = z * Math.Sin(theta);
            return new GPSPosition()
            {
                Latitude = gcjLat,
                Longitude = gcjLon
            };
        }

        /// 
        /// WGS84 转 GCJ02
        /// 
        /// 
        /// 
        /// 
        public static GPSPosition WGS84ToGCJ02(double lng, double lat)
        {
            double dLat = transformLat(lng - 105.0, lat - 35.0);
            double dLon = transformLon(lng - 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);
            double mgLat = lat + dLat;
            double mgLon = lng + dLon;
            return new GPSPosition()
            {
                Latitude = mgLat,
                Longitude = mgLon
            };
        }

        /// 
        /// WGS84 转 BD09
        /// 
        /// 
        /// 
        /// 
        public static GPSPosition WGS84ToBD09(double lng, double lat)
        {
            var temp = WGS84ToGCJ02(lng, lat);
            return GCJ02ToBD09(temp.Longitude, temp.Latitude);
        }

        static dynamic gcj_encrypt(double wgsLat, double wgsLon)
        {
            if (outOfChina(wgsLat, wgsLon))
                return new { lat = wgsLat, lon = wgsLon };

            var d = delta(wgsLat, wgsLon);
            return new { lat = wgsLat + d.lat, lon = wgsLon + d.lon };
        }

        static dynamic 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;
        }

        static dynamic delta(double lat, double lon)
        {
            // Krasovsky 1940
            //
            // a = 6378245.0, 1/f = 298.3
            // b = a * (1 - f)
            // ee = (a^2 - b^2) / a^2;
            var dLat = transformLat(lon - 105.0, lat - 35.0);
            var dLon = transformLon(lon - 105.0, lat - 35.0);
            var radLat = lat / 180.0 * PI;
            var magic = Math.Sin(radLat);
            magic = 1 - ee * magic * magic;
            var sqrtMagic = Math.Sqrt(magic);
            dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI);
            dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * PI);
            return new { lat = dLat, lon = dLon };
        }

        static double transformLat(double x, double y)
        {
            var 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;
        }

        static double transformLon(double x, double y)
        {
            var 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;
        }
    }

你可能感兴趣的:(其他存档)