public static ChinaArea GetNearPosition(MapPoint point) { var key = Thread.CurrentThread.ManagedThreadId; List <ChinaArea> list = null; ChinaArea minitem = null; var min = double.MaxValue; var radlat = Rad(point.Y); var radlon = Rad(point.X); if (PositionDic.TryGetValue(key, out list)) { list = list.Select(p => p).ToList(); foreach (var item in list) { if (item is Province) { var cities = (item as Province).Cities; if (cities == null) { foreach (var pts in item.MapPointsList) { foreach (var pt in pts) { var dis = CalDistance2(radlat, radlon, Rad(pt.Y), Rad(pt.X)); if (dis < min) { min = dis; minitem = item; } } } continue; } foreach (var city in cities) { var ct = city.Position(point); if (ct != null) { return(ct); } foreach (var pts in city.MapPointsList) { foreach (var pt in pts) { var dis = CalDistance2(radlat, radlon, Rad(pt.Y), Rad(pt.X)); if (dis < min) { min = dis; minitem = city; } } } } } } if (min < 100000) { return(minitem); } return(null); } else { return(null); } }
/// <summary> /// 判断一个点是否在多边形区域内 /// </summary> /// <param name="points"></param> /// <param name="point"></param> /// <returns></returns> public bool IsInArea(MapPoint[] points, double minxscral, double minyscral, MapPoint point) { if (points == null || points.Length < 2) { return(false); } List <double> ylist = new List <double>(); List <double> xlist = new List <double>(); MapPoint p1 = null, p2 = null; double crossX = double.MinValue, crossY = double.MinValue; for (int i = 0; i < points.Length - 1; i++) { p1 = points[i]; p2 = points[i + 1]; if (minxscral == 0 || Math.Abs(p1.X - point.X) <= minxscral) { if ((p1.X <= point.X && point.X <= p2.X) || (p2.X <= point.X && point.X <= p1.X)) { if (p1.X != p2.X) { crossY = p1.Y + ((p2.Y - p1.Y) / Math.Abs(p2.X - p1.X)) * Math.Abs(p1.X - point.X); } else { if ((p1.Y - point.Y) * (p2.Y - point.Y) <= 0) { crossY = point.Y; } else { crossY = p1.Y + (p2.Y - p1.Y) / 2; } } ylist.Add(crossY); } } if (minyscral == 0 || Math.Abs(p1.Y - point.Y) <= minyscral) { if ((p1.Y <= point.Y && p2.Y >= point.Y) || (p2.Y <= point.Y && p1.Y >= point.Y)) { if (p1.Y != p2.Y) { crossX = p1.X + (p2.X - p1.X) / Math.Abs(p2.Y - p1.Y) * Math.Abs(p1.Y - point.Y); } else { if ((p1.X - point.X) * (p2.X - point.X) <= 0) { crossX = point.X; } else { crossX = p1.X + (p2.X - p1.X) / 2; } } xlist.Add(crossX); } } } if (xlist.Count == 0 || ylist.Count == 0) { return(false); } xlist = xlist.Distinct().OrderBy(p => p).ToList(); ylist = ylist.Distinct().OrderBy(p => p).ToList(); for (int i = 0; i < xlist.Count; i++) { var curr = xlist[i]; if (point.X < curr) { if (i % 2 == 0) { return(false); } else { break; } } else if (i == xlist.Count - 1 && point.X > curr) { return(false); } } for (int j = 0; j < ylist.Count; j++) { var curr = ylist[j]; if (point.Y < curr) { if (j % 2 == 0) { return(false); } else { return(true); } } else if (j == ylist.Count - 1 && point.Y > curr) { return(false); } } return(true); }