| | |
| | | } |
| | | } |
| | | |
| | | #region 高德坐标转WGS84坐标 |
| | | /// <summary> |
| | | /// 高德坐标转WGS84坐标 |
| | | /// </summary> |
| | | /// <param name="in_lng"></param> |
| | | /// <param name="in_lat"></param> |
| | | /// <param name="out_lng"></param> |
| | | /// <param name="out_lat"></param> |
| | | public void GCJ02_to_WGS84(double in_lng, double in_lat, out double out_lng, out double out_lat) |
| | | { |
| | | if (OutOfChina(in_lat, in_lng)) |
| | | { |
| | | out_lng = in_lng; |
| | | out_lat = in_lat; |
| | | return; |
| | | } |
| | | |
| | | CalculateDev(in_lng, in_lat, out out_lng, out out_lat); |
| | | out_lng = in_lng - out_lng; |
| | | out_lat = in_lat - out_lat; |
| | | } |
| | | #endregion |
| | | |
| | | #region WGS84坐标转高德坐标 |
| | | /// <summary> |
| | | /// WGS84坐标转高德坐标 |
| | | /// </summary> |
| | | /// <param name="in_lng">经度</param> |
| | | /// <param name="in_lat">纬度</param> |
| | | /// <param name="out_lng"></param> |
| | | /// <param name="out_lat"></param> |
| | | public void WGS84_to_GCJ02(double in_lng, double in_lat, out double out_lng, out double out_lat) |
| | | { |
| | | if (OutOfChina(in_lat, in_lng)) |
| | | { |
| | | out_lng = in_lng; |
| | | out_lat = in_lat; |
| | | return; |
| | | } |
| | | |
| | | CalculateDev(in_lng, in_lat, out out_lng, out out_lat); |
| | | out_lng = in_lng + out_lng; |
| | | out_lat = in_lat + out_lat; |
| | | } |
| | | #endregion |
| | | |
| | | /// <summary> |
| | | /// 坐标是否在中国境内 |
| | | /// </summary> |
| | | /// <param name="lat"></param> |
| | | /// <param name="lng"></param> |
| | | /// <returns></returns> |
| | | public bool OutOfChina(double lat, double lng) |
| | | { |
| | | if (lng < 72.004 || lng > 137.8347) |
| | | return true; |
| | | if (lat < 0.8293 || lat > 55.8271) |
| | | return true; |
| | | return false; |
| | | } |
| | | /// <summary> |
| | | /// 计算偏差 |
| | | /// </summary> |
| | | /// <param name="in_lng"></param> |
| | | /// <param name="in_lat"></param> |
| | | /// <param name="dLng"></param> |
| | | /// <param name="dLat"></param> |
| | | private void CalculateDev(double in_lng, double in_lat, out double dLng, out double dLat) |
| | | { |
| | | dLat = TransformLat(in_lng - 105.0, in_lat - 35.0); |
| | | dLng = TransformLng(in_lng - 105.0, in_lat - 35.0); |
| | | |
| | | double radLat = in_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); |
| | | dLng = (dLng * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi); |
| | | } |
| | | |
| | | private 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 double TransformLng(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; |
| | | } |
| | | // 椭球参数-圆周率 |
| | | private const double pi = 3.14159265358979324; |
| | | |
| | | // (北京54)椭球长半轴,卫星椭球坐标投影到平面地图坐标系的投影因子 |
| | | private const double a = 6378245.0; |
| | | /* |
| | | * Krasovsky 1940 (北京54)椭球长半轴第一偏心率平方 |
| | | * 计算方式: |
| | | * 长半轴: |
| | | * a = 6378245.0 |
| | | * 扁率: |
| | | * 1/f = 298.3(变量相关计算为:(a-b)/a) |
| | | * 短半轴: |
| | | * b = 6356863.0188 (变量相关计算方法为:b = a * (1 - f)) |
| | | * 第一偏心率平方: |
| | | * e2 = (a^2 - b^2) / a^2; |
| | | */ |
| | | private const double ee = 0.00669342162296594323; |
| | | |
| | | /// <summary> |
| | | /// APP上报GPS经纬度 |
| | | /// </summary> |
| | | public void AppLatAndLonEvent() |
| | | { |
| | | Application.LocationAction += (lon, lat) => |
| | | { |
| | | ////GPS坐标转成高德坐标 |
| | | //double out_lng, out_lat; |
| | | //this.WGS84_to_GCJ02(lon, lat, out out_lng, out out_lat); |
| | | //上报经纬度 |
| | | this.AutomatedGeofenceStatusReporting(lon, lat); |
| | | }; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 自动化地理围栏状态上报云端 |
| | | /// </summary> |
| | | /// <param name="out_lng">APP经度</param> |
| | | /// <param name="out_lat">APP纬度</param> |
| | | private void AutomatedGeofenceStatusReporting(double out_lng, double out_lat) |
| | | { |
| | | List<LogicData> logicDataList = new List<LogicData>(); |
| | | //获取逻辑ID列表 |
| | | var idStr = Send.GetLogicIdList(); |
| | | if (idStr.Code == "0" && idStr.Data != null && idStr.Data.ToString() != "") |
| | | { |
| | | var date = Newtonsoft.Json.JsonConvert.SerializeObject(idStr.Data); |
| | | logicDataList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<LogicData>>(date); |
| | | } |
| | | ///有自动列表才处理 |
| | | if (logicDataList.Count>0) { |
| | | ///遍历所有列表 |
| | | for (int i = 0; i < logicDataList.Count; i++) |
| | | { |
| | | var logicDate = logicDataList[i]; |
| | | ///自动化没有配置地理围栏不处理 |
| | | if (string.IsNullOrEmpty(logicDate.geo_fence.latitude) || string.IsNullOrEmpty(logicDate.geo_fence.longitude)) |
| | | { |
| | | //经纬度为空,不处理 |
| | | continue; |
| | | } |
| | | //自动化纬度 |
| | | double lat = Convert.ToDouble(logicDate.geo_fence.latitude); |
| | | //自动化经度 |
| | | double lon = Convert.ToDouble(logicDate.geo_fence.longitude); |
| | | //自动化半径<单位米> |
| | | int radius = int.Parse(logicDate.geo_fence.radius); |
| | | //计算2个经纬度之间的距离 |
| | | int r =Infrastructure.Service.Helper.CalculatedDistance.Distance(out_lat, out_lng, lat, lon); |
| | | //if () { } |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | } |