/// /// 判斷經緯度是否在範圍類 /// /// 經度 /// 緯度 /// /// public static bool IsInRegion(double longitudeCur, double latitudeCur, IList pathList... ...
/// <summary> /// 判斷經緯度是否在範圍類 /// </summary> /// <param name="longitudeCur">經度</param> /// <param name="latitudeCur">緯度</param> /// <param name="pathList"></param> /// <returns></returns> public static bool IsInRegion(double longitudeCur, double latitudeCur, IList<RegionPath> pathList) { if (pathList.Count < 3)//點小於3無法構成多邊形 { return false; } int iSum = 0; int pathCount = pathList.Count; double longStart = 0, latiStart = 0, longEnd = 0, latiEnd = 0; double dLong = 0; for (int i = 0; i < pathCount; i++) { int nextIndex = i + 1; if (i == pathCount - 1) { nextIndex = 0; } longStart = pathList[i].Longitude; latiStart = pathList[i].Latitude; longEnd = pathList[nextIndex].Longitude; latiEnd = pathList[nextIndex].Latitude; //判斷緯度即Y坐標是否在2點的Y坐標內,只有在其內水平線才會相交 if ((latitudeCur >= latiStart && latitudeCur < latiEnd) || (latitudeCur >= latiEnd && latitudeCur < latiStart)) { if (Math.Abs(latiStart-latiEnd)>0) { dLong = longStart - ((longStart - longEnd) * (latiStart - latitudeCur)) / (latiStart - latiEnd); if (dLong < longitudeCur) { iSum++; } } } } if ((iSum % 2) != 0) { return true; } return false; }