measureArea.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. var measureArea = {
  2. modValue: {
  3. handler: null,
  4. tempPoints: [],
  5. viewer: null,
  6. tempEntities: {},
  7. areaIndex: 0,
  8. area: null,
  9. polygon: {},
  10. clampToGround: false,
  11. tips: $('<div class="measureArea_tips map3D">左键添加点,右键结束</div>')
  12. },
  13. init: function (options) {
  14. if (options.action == 'add') {
  15. this.modValue.viewer = options.viewer;
  16. this.modValue.clampToGround = options.clampToGround;
  17. this.addMeasureArea();
  18. this.modValue.tips.appendTo(this.modValue.viewer._container);
  19. } else if (options.action == 'remove') {
  20. this.removeMeasureArea();
  21. }
  22. },
  23. addMeasureArea: function () {
  24. var _this = this
  25. if (!this.modValue.handler) {
  26. this.modValue.tempPoints = [];
  27. this.modValue.areaIndex++;
  28. this.modValue.tempEntities[this.modValue.areaIndex] = [];
  29. this.modValue.handler = new SkyScenery.ScreenSpaceEventHandler(this.modValue.viewer.scene.canvas);
  30. this.modValue.handler.setInputAction(function (event) {
  31. var wp = event.endPosition;
  32. if (!SkyScenery.defined(wp)) {
  33. return;
  34. }
  35. var ray = _this.modValue.viewer.camera.getPickRay(wp);
  36. if (!SkyScenery.defined(ray)) {
  37. return;
  38. }
  39. var cartesian = _this.modValue.viewer.scene.globe.pick(ray, _this.modValue.viewer.scene);
  40. if (!SkyScenery.defined(cartesian)) {
  41. return;
  42. }
  43. $('#' + _this.modValue.viewer._container.id + ' .measureArea_tips').css({
  44. "left": wp.x + 20,
  45. 'top': wp.y + 10
  46. })
  47. }, SkyScenery.ScreenSpaceEventType.MOUSE_MOVE);
  48. this.modValue.handler.setInputAction(function (click) {
  49. var latlng = _this.screenToLatLng(click.position.x, click.position.y)
  50. if (latlng) {
  51. _this.modValue.tempPoints.push({
  52. lon: latlng.lng,
  53. lat: latlng.lat,
  54. alt: latlng.alt
  55. });
  56. var tempLength = _this.modValue.tempPoints.length;
  57. _this.drawPoint(_this.modValue.tempPoints[_this.modValue.tempPoints.length - 1]);
  58. if (tempLength > 2) {
  59. _this.drawPoly(_this.modValue.tempPoints);
  60. }
  61. }
  62. }, SkyScenery.ScreenSpaceEventType.LEFT_CLICK);
  63. this.modValue.handler.setInputAction(function (click) {
  64. var cartesian = _this.modValue.viewer.camera.pickEllipsoid(click.position, _this.modValue.viewer.scene.globe.ellipsoid);
  65. if (cartesian) {
  66. var tempLength = _this.modValue.tempPoints.length;
  67. if (tempLength < 3) {
  68. alert('请选择3个以上的点再执行闭合操作命令');
  69. } else {
  70. _this.drawPoly(_this.modValue.tempPoints);
  71. // highLightAssetsInArea(_this.this.modValue.tempPoints)
  72. var ent =
  73. _this.modValue.viewer.entities.add({
  74. position: SkyScenery.Cartesian3.fromDegrees(_this.modValue.tempPoints[_this.modValue.tempPoints.length - 1].lon, _this.modValue.tempPoints[_this.modValue.tempPoints.length - 1].lat),
  75. label: {
  76. text: _this.SphericalPolygonAreaMeters(_this.modValue.tempPoints),
  77. font: '22px Helvetica',
  78. heightReference: 2,
  79. pixelOffset: new SkyScenery.Cartesian2(0, -22),
  80. fillColor: SkyScenery.Color.WHITE,
  81. verticalOrigin: SkyScenery.VerticalOrigin.BOTTOM,
  82. }
  83. });
  84. _this.modValue.tempEntities[_this.modValue.areaIndex].push(ent);
  85. _this.modValue.tempPoints = [];
  86. _this.removeMeasureArea('', true);
  87. }
  88. }
  89. }, SkyScenery.ScreenSpaceEventType.RIGHT_CLICK);
  90. }
  91. },
  92. removeMeasureArea: function (callback) {
  93. if (!this.modValue.viewer) {
  94. return;
  95. }
  96. $('#' + this.modValue.viewer._container.id + ' .measureArea_tips').remove();
  97. if (this.modValue.handler) {
  98. this.modValue.tempPoints = [];
  99. this.modValue.handler.destroy();
  100. this.modValue.handler = null;
  101. }
  102. if (callback) {
  103. this.init('', {
  104. action: 'add',
  105. viewer: this.modValue.viewer
  106. })
  107. }
  108. },
  109. drawPoint: function (point) {
  110. var entity =
  111. this.modValue.viewer.entities.add({
  112. position: SkyScenery.Cartesian3.fromDegrees(point.lon, point.lat),
  113. point: {
  114. pixelSize: 10,
  115. heightReference: 2,
  116. color: SkyScenery.Color.RED,
  117. }
  118. });
  119. this.modValue.tempEntities[this.modValue.areaIndex].push(entity);
  120. },
  121. drawPoly: function (points) {
  122. var pArray = [];
  123. for (var i = 0; i <= points.length; i++) {
  124. if (i < points.length) {
  125. pArray.push(points[i].lon);
  126. pArray.push(points[i].lat);
  127. pArray.push(points[i].alt);
  128. }
  129. }
  130. if (this.modValue.polygon[this.modValue.areaIndex]) {
  131. this.modValue.polygon[this.modValue.areaIndex].polygon.hierarchy.setValue(new SkyScenery.PolygonHierarchy(SkyScenery.Cartesian3.fromDegreesArrayHeights(pArray)));
  132. } else {
  133. this.modValue.polygon[this.modValue.areaIndex] = this.modValue.viewer.entities.add({
  134. polygon: {
  135. hierarchy: new SkyScenery.PolygonHierarchy(SkyScenery.Cartesian3.fromDegreesArrayHeights(pArray)),
  136. heightReference:1,
  137. //height : 5,
  138. material: SkyScenery.Color.CHARTREUSE.withAlpha(0.5),
  139. outline: true,
  140. outlineColor: SkyScenery.Color.YELLOW,
  141. outlineWidth: 2
  142. }
  143. });
  144. this.modValue.tempEntities[this.modValue.areaIndex].push(this.modValue.polygon[this.modValue.areaIndex]);
  145. }
  146. },
  147. SphericalPolygonAreaMeters: function (points) {
  148. var latLngs = points;
  149. var pointsCount = latLngs.length,
  150. area = 0.0,
  151. d2r = Math.PI / 180,
  152. p1, p2;
  153. if (pointsCount > 2) {
  154. for (var i = 0; i < pointsCount; i++) {
  155. p1 = latLngs[i];
  156. p2 = latLngs[(i + 1) % pointsCount];
  157. area += ((p2.lon - p1.lon) * d2r) *
  158. (2 + Math.sin(p1.lat * d2r) + Math.sin(p2.lat * d2r));
  159. }
  160. area = area * 6378137.0 * 6378137.0 / 2.0;
  161. }
  162. area = Math.abs(area);
  163. if (area > 1000000) {
  164. area = (area * 0.000001).toFixed(2) + ' 平方公里';
  165. } else {
  166. area = area.toFixed(2) + '平方米';
  167. }
  168. return area;
  169. },
  170. screenToLatLng: function (x, y) {
  171. var pick1 = new SkyScenery.Cartesian2(x, y);
  172. var cartesian = this.modValue.viewer.scene.globe.pick(this.modValue.viewer.camera.getPickRay(pick1), this.modValue.viewer.scene);
  173. if (cartesian) {
  174. var cartographic = this.modValue.viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian);
  175. var lat = SkyScenery.Math.toDegrees(cartographic.latitude);
  176. var lng = SkyScenery.Math.toDegrees(cartographic.longitude);
  177. var alt = cartographic.height;
  178. var position = {
  179. lat: lat,
  180. lng: lng,
  181. alt: alt
  182. }
  183. } else {
  184. var position = false
  185. }
  186. return position;
  187. },
  188. clear: function (options) {
  189. var _this = this
  190. $.each(this.modValue.tempEntities, function (i, t) {
  191. for (var j = 0; j < t.length; j++) {
  192. _this.modValue.viewer.entities.removeById(t[j]._id);
  193. }
  194. })
  195. }
  196. }