LBS坐标转换,支持Wgs84、Gcj02、Bd09三种坐标系的互转。
public class GpsCoordinate { #region 地球参数 private static readonly double pi = 3.1415926535897932384626; private static readonly double a = 6378245.0; private static readonly double ee = 0.00669342162296594323; #endregion #region 坐标转换公开方法 /// <summary> /// 坐标类型转换 /// </summary> public enum LBS_TYPE { Wgs84_To_Gcj02 = 1, Gcj02_To_Wgs84 = 2, Gcj02_To_Bd09 = 3, Bd09_To_Gcj02 = 4, Bd09_To_Wgs84 = 5, Wgs84_To_Bd09 = 6 } /// <summary> /// 坐标类型 /// </summary> public enum GPS_TYPE { [Description ("地球坐标系:标准GPS")] Wgs84 = 1, [Description ("火星坐标系:高德")] Gcj02 = 2, [Description ("高德坐标转地名:百度")] Bd09 = 3 } /// <summary> /// 坐标转换 /// </summary> /// <param name="lat">纬度</param> /// <param name="lon">经度</param> /// <param name="OldTyle">旧坐标系</param> /// <param name="NewType">新坐标系</param> /// <returns></returns> public static Gps GpsToNewGps (double lat, double lon, GPS_TYPE OldTyle, GPS_TYPE NewType) { int OldTyle_1 = (int) OldTyle; int NewType_1 = (int) NewType; Gps gs = null; switch (OldTyle_1 + "" + NewType_1) { case "12": gs = Wgs84_To_Gcj02 (lat, lon); break; case "21": gs = Gcj02_To_Wgs84 (lat, lon); break; case "23": gs = Gcj02_To_Bd09 (lat, lon); break; case "32": gs = Bd09_To_Gcj02 (lat, lon); break; case "31": gs = Bd09_To_Wgs84 (lat, lon); break; case "13": gs = Wgs84_To_Bd09 (lat, lon); break; default: gs = new Gps (lat, lon); break; } return gs; } /// <summary> /// 坐标转换 /// </summary> /// <param name="lat">纬度</param> /// <param name="lon">经度</param> /// <param name="type">转换类型(enum LBS_TYPE)</param> /// <returns></returns> public static Gps GpsToNewGps (double lat, double lon, LBS_TYPE type) { /** * type=1;Wgs84 to 火星坐标系 (GCJ-02) * type=2;火星坐标系 (GCJ-02) to Wgs84 * type=3;火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) * type=4;(BD-09)-->火星坐标系 (GCJ-02) * type=5;(BD-09)-->Wgs84 * **/ Gps gs = null; switch (type) { case LBS_TYPE.Wgs84_To_Gcj02: gs = Wgs84_To_Gcj02 (lat, lon); break; case LBS_TYPE.Gcj02_To_Wgs84: gs = Gcj02_To_Wgs84 (lat, lon); break; case LBS_TYPE.Gcj02_To_Bd09: gs = Gcj02_To_Bd09 (lat, lon); break; case LBS_TYPE.Bd09_To_Gcj02: gs = Bd09_To_Gcj02 (lat, lon); break; case LBS_TYPE.Bd09_To_Wgs84: gs = Bd09_To_Wgs84 (lat, lon); break; case LBS_TYPE.Wgs84_To_Bd09: gs = Wgs84_To_Bd09 (lat, lon); break; default: gs = new Gps (lat, lon); break; } return gs; } /// <summary> /// 坐标转换 /// </summary> /// <param name="lat">纬度</param> /// <param name="lon">经度</param> /// <param name="type">[1;Wgs84 to 火星坐标系 (GCJ-02)][2;火星坐标系 (GCJ-02) to Wgs84 ][3;火星坐标系 (GCJ-02) 与百度坐标系 (BD-09)][4;(BD-09)-->火星坐标系 (GCJ-02)][5;(BD-09)-->Wgs84]</param> /// <returns>Gps</returns> public static Gps GpsToNewGps (double lat, double lon, string type) { /** * type=1;Wgs84 to 火星坐标系 (GCJ-02) * type=2;火星坐标系 (GCJ-02) to Wgs84 * type=3;火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) * type=4;(BD-09)-->火星坐标系 (GCJ-02) * type=5;(BD-09)-->Wgs84 * **/ Gps gs = null; switch (type) { case "1": gs = Wgs84_To_Gcj02 (lat, lon); break; case "2": gs = Gcj02_To_Wgs84 (lat, lon); break; case "3": gs = Gcj02_To_Bd09 (lat, lon); break; case "4": gs = Bd09_To_Gcj02 (lat, lon); break; case "5": gs = Bd09_To_Wgs84 (lat, lon); break; case "6": gs = Wgs84_To_Bd09 (lat, lon); break; default: gs = new Gps (lat, lon); break; } return gs; } /// <summary> /// 坐标转换 /// Wgs84 to 火星坐标系 (GCJ-02):当返回null是标识坐标 /// </summary> /// <param name="lat">纬度</param> /// <param name="lon">经度</param> /// <param name="type">[1;Wgs84 to 火星坐标系 (GCJ-02)][2;火星坐标系 (GCJ-02) to Wgs84 ][3;火星坐标系 (GCJ-02) 与百度坐标系 (BD-09)][4;(BD-09)-->火星坐标系 (GCJ-02)][5;(BD-09)-->Wgs84]</param> /// <returns>Gps</returns> public static Gps GpsToNewGps (double lat, double lon, int type) { /** * type=1;Wgs84 to 火星坐标系 (GCJ-02) * type=2;火星坐标系 (GCJ-02) to Wgs84 * type=3;火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) * type=4;(BD-09)-->火星坐标系 (GCJ-02) * type=5;(BD-09)-->Wgs84 * **/ Gps gs = null; switch (type) { case 1: gs = Wgs84_To_Gcj02 (lat, lon); break; case 2: gs = Gcj02_To_Wgs84 (lat, lon); break; case 3: gs = Gcj02_To_Bd09 (lat, lon); break; case 4: gs = Bd09_To_Gcj02 (lat, lon); break; case 5: gs = Bd09_To_Wgs84 (lat, lon); break; case 6: gs = Wgs84_To_Bd09 (lat, lon); break; default: gs = new Gps (lat, lon); break; } return gs; } #endregion #region 坐标转化 /// <summary> ///Wgs84==>>GCJ-02 /// </summary> private static Gps Wgs84_To_Gcj02 (double lat, double lon) { //if (outOfChina(lat, lon)) //{ // return new Gps(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); double mgLat = lat + dLat; double mgLon = lon + dLon; return new Gps (mgLat, mgLon); } /// <summary> /// GCJ-02==>>Wgs84 /// </summary> private static Gps Gcj02_To_Wgs84 (double lat, double lon) { Gps gps = transform (lat, lon); double lontitude = lon * 2 - gps.getWgLon (); double latitude = lat * 2 - gps.getWgLat (); return new Gps (latitude, lontitude); } /// <summary> /// GCJ-02==>>BD-09 /// </summary> private static Gps Gcj02_To_Bd09 (double gg_lat, double gg_lon) { double x = gg_lon, y = gg_lat; double z = Math.Sqrt (x * x + y * y) + 0.00002 * Math.Sin (y * pi); double theta = Math.Atan2 (y, x) + 0.000003 * Math.Cos (x * pi); double bd_lon = z * Math.Cos (theta) + 0.0065; double bd_lat = z * Math.Sin (theta) + 0.006; return new Gps (bd_lat, bd_lon); } /// <summary> /// BD-09==>>GCJ-02 /// </summary> private static Gps Bd09_To_Gcj02 (double bd_lat, double bd_lon) { double x = bd_lon - 0.0065, y = bd_lat - 0.006; double z = Math.Sqrt (x * x + y * y) - 0.00002 * Math.Sin (y * pi); double theta = Math.Atan2 (y, x) - 0.000003 * Math.Cos (x * pi); double gg_lon = z * Math.Cos (theta); double gg_lat = z * Math.Sin (theta); return new Gps (gg_lat, gg_lon); } /// <summary> /// (BD-09)==>>Wgs84 /// </summary> private static Gps Bd09_To_Wgs84 (double bd_lat, double bd_lon) { Gps gcj02 = Bd09_To_Gcj02 (bd_lat, bd_lon); Gps map84 = Gcj02_To_Wgs84 (gcj02.getWgLat (), gcj02.getWgLon ()); return map84; } /// <summary> /// Wgs84==>>(BD-09) /// </summary> /// <param name="bd_lat"></param> /// <param name="bd_lon"></param> /// <returns></returns> private static Gps Wgs84_To_Bd09 (double bd_lat, double bd_lon) { Gps gcj02 = Wgs84_To_Gcj02 (bd_lat, bd_lon); Gps map84 = Gcj02_To_Bd09 (gcj02.getWgLat (), gcj02.getWgLon ()); return map84; } #endregion #region 公共方法 /// <summary> /// 是否在中国内 /// </summary> /// <param name="lat"></param> /// <param name="lon"></param> /// <returns></returns> 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; } private static Gps transform (double lat, double lon) { //if (outOfChina(lat, lon)) //{ // return new Gps(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); double mgLat = lat + dLat; double mgLon = lon + dLon; return new Gps (mgLat, mgLon); } private 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; } private 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; } #endregion } public class Gps { /// <summary> /// 纬度 /// </summary> private double wgLat; /// <summary> /// 经度 /// </summary> private double wgLon; public Gps () { wgLat = 0; wgLon = 0; } public Gps (double wgLat, double wgLon) { setWgLat (wgLat); setWgLon (wgLon); } public double getWgLat () { return wgLat; } public void setWgLat (double wgLat) { this.wgLat = wgLat; } public double getWgLon () { return wgLon; } public void setWgLon (double wgLon) { this.wgLon = wgLon; } public String toString () { return wgLat + "," + wgLon; } }