//地球半径,单位米 private const double EARTH_RADIUS = 6378137; /// <summary> /// 计算两点位置的距离,返回两点的距离,单位:米 /// 该公式为GOOGLE提供,误差小于0.2米 /// </summary> /// <param name="lng1">第一点经度</param> /// <param name="lat1">第一点纬度</param> /// <param name="lng2">第二点经度</param> /// <param name="lat2">第二点纬度</param> /// <returns></returns> public static double GetDistance(double lng1, double lat1, double lng2, double lat2) { double radLat1 = Rad(lat1); double radLng1 = Rad(lng1); double radLat2 = Rad(lat2); double radLng2 = Rad(lng2); double a = radLat1 - radLat2; double b = radLng1 - radLng2; double result = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2))) * EARTH_RADIUS; return result; //return Math.Round((result / 1000d), 2); } /// <summary> /// 经纬度转化成弧度 /// </summary> /// <param name="d"></param> /// <returns></returns> private static double Rad(double d) { return (double)d * Math.PI / 180d; }
通过GeoServer判断点是否在面内(一定要在GeoServer上发布了面服务)
/// <summary> /// 判断点是否在面内 /// </summary> /// <returns></returns> public bool isIn(double JINGDU, double WEIDU) { bool r = false; string url = $"http://XXX.XXX.XX.XX:3939/geoserver/LIANCHENGDIANZIWEILAN/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=LIANCHENGDIANZIWEILAN%3A%E5%8A%9F%E8%83%BD%E5%8C%BA%E5%88%92&maxFeatures=50&outputFormat=application%2Fjson&cql_filter=INTERSECTS(the_geom,POINT( {JINGDU} {WEIDU}))"; WebClient client = new WebClient(); try { client.Encoding = Encoding.UTF8; JObject jo = JObject.Parse(client.DownloadString(url)); var t = jo["totalFeatures"]; if (t.ToString() != "0") { r = true; } //string result = JObject.Parse(client.DownloadString(url)); } catch (Exception ex) { //Log4netHelper.Info($"当前时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} 判断点是否在面内服务失败,TaskID为:"); } return r; }
注意:一定要有maxFeatures参数,否则会查询不出来的数据(血的教训)
后面的cql_filter=INTERSECTS就是查询判断