1 //参数是一个百度类型的多边形覆盖物 2 function getArea(polygon){ 3 4 // 检查类型:既不是百度类型的范围又不是数组类型的数据,直接返回0 5 if (!(polygon instanceof BMap.Polygon) && !(polygon instanceof Array)) { 6 return 0; 7 } 8 9 //如果是百度类型的,得到点集合,不是的话,另说。 10 var pts = new Array(); 11 if (polygon instanceof BMap.Polygon) { 12 pts = polygon.getPath(); 13 } 14 15 //判断数组的长度,如果是小于3的话,不构成面,无法计算面积 16 if (pts.length < 3) { 17 return 0; 18 } 19 20 var totalArea = 0;// 初始化总面积 21 var LowX = 0.0; 22 var LowY = 0.0; 23 var MiddleX = 0.0; 24 var MiddleY = 0.0; 25 var HighX = 0.0; 26 var HighY = 0.0; 27 var AM = 0.0; 28 var BM = 0.0; 29 var CM = 0.0; 30 var AL = 0.0; 31 var BL = 0.0; 32 var CL = 0.0; 33 var AH = 0.0; 34 var BH = 0.0; 35 var CH = 0.0; 36 var CoefficientL = 0.0; 37 var CoefficientH = 0.0; 38 var ALtangent = 0.0; 39 var BLtangent = 0.0; 40 var CLtangent = 0.0; 41 var AHtangent = 0.0; 42 var BHtangent = 0.0; 43 var CHtangent = 0.0; 44 var ANormalLine = 0.0; 45 var BNormalLine = 0.0; 46 var CNormalLine = 0.0; 47 var OrientationValue = 0.0; 48 var AngleCos = 0.0; 49 var Sum1 = 0.0; 50 var Sum2 = 0.0; 51 var Count2 = 0; 52 var Count1 = 0; 53 var Sum = 0.0; 54 var Radius = 6378137.0;// ,WGS84椭球半径 55 var Count = pts.length; 56 for ( var i = 0; i < Count; i++) { 57 if (i == 0) { 58 LowX = pts[Count - 1].lng * Math.PI / 180; 59 LowY = pts[Count - 1].lat * Math.PI / 180; 60 MiddleX = pts[0].lng * Math.PI / 180; 61 MiddleY = pts[0].lat * Math.PI / 180; 62 HighX = pts[1].lng * Math.PI / 180; 63 HighY = pts[1].lat * Math.PI / 180; 64 } else if (i == Count - 1) { 65 LowX = pts[Count - 2].lng * Math.PI / 180; 66 LowY = pts[Count - 2].lat * Math.PI / 180; 67 MiddleX = pts[Count - 1].lng * Math.PI / 180; 68 MiddleY = pts[Count - 1].lat * Math.PI / 180; 69 HighX = pts[0].lng * Math.PI / 180; 70 HighY = pts[0].lat * Math.PI / 180; 71 } else { 72 LowX = pts[i - 1].lng * Math.PI / 180; 73 LowY = pts[i - 1].lat * Math.PI / 180; 74 MiddleX = pts[i].lng * Math.PI / 180; 75 MiddleY = pts[i].lat * Math.PI / 180; 76 HighX = pts[i + 1].lng * Math.PI / 180; 77 HighY = pts[i + 1].lat * Math.PI / 180; 78 } 79 AM = Math.cos(MiddleY) * Math.cos(MiddleX); 80 BM = Math.cos(MiddleY) * Math.sin(MiddleX); 81 CM = Math.sin(MiddleY); 82 AL = Math.cos(LowY) * Math.cos(LowX); 83 BL = Math.cos(LowY) * Math.sin(LowX); 84 CL = Math.sin(LowY); 85 AH = Math.cos(HighY) * Math.cos(HighX); 86 BH = Math.cos(HighY) * Math.sin(HighX); 87 CH = Math.sin(HighY); 88 CoefficientL = (AM * AM + BM * BM + CM * CM)/ (AM * AL + BM * BL + CM * CL); 89 CoefficientH = (AM * AM + BM * BM + CM * CM)/ (AM * AH + BM * BH + CM * CH); 90 ALtangent = CoefficientL * AL - AM; 91 BLtangent = CoefficientL * BL - BM; 92 CLtangent = CoefficientL * CL - CM; 93 AHtangent = CoefficientH * AH - AM; 94 BHtangent = CoefficientH * BH - BM; 95 CHtangent = CoefficientH * CH - CM; 96 AngleCos = (AHtangent * ALtangent + BHtangent * BLtangent + CHtangent* CLtangent)/ (Math.sqrt(AHtangent * AHtangent + BHtangent* BHtangent + CHtangent * CHtangent) * Math.sqrt(ALtangent * ALtangent + BLtangent* BLtangent + CLtangent * CLtangent)); 97 AngleCos = Math.acos(AngleCos); 98 ANormalLine = BHtangent * CLtangent - CHtangent * BLtangent; 99 BNormalLine = 0 - (AHtangent * CLtangent - CHtangent* ALtangent); 100 CNormalLine = AHtangent * BLtangent - BHtangent * ALtangent; 101 if (AM != 0) 102 OrientationValue = ANormalLine / AM; 103 else if (BM != 0) 104 OrientationValue = BNormalLine / BM; 105 else 106 OrientationValue = CNormalLine / CM; 107 if (OrientationValue > 0) { 108 Sum1 += AngleCos; 109 Count1++; 110 } else { 111 Sum2 += AngleCos; 112 Count2++; 113 } 114 } 115 116 var tempSum1, tempSum2; 117 tempSum1 = Sum1 + (2 * Math.PI * Count2 - Sum2); 118 tempSum2 = (2 * Math.PI * Count1 - Sum1) + Sum2; 119 if (Sum1 > Sum2) { 120 if ((tempSum1 - (Count - 2) * Math.PI) < 1) 121 Sum = tempSum1; 122 else 123 Sum = tempSum2; 124 } else { 125 if ((tempSum2 - (Count - 2) * Math.PI) < 1) 126 Sum = tempSum2; 127 else 128 Sum = tempSum1; 129 } 130 totalArea = (Sum - (Count - 2) * Math.PI) * Radius * Radius; 131 132 return totalArea; // 返回总面积 133 }