DrawPolygon.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // 六、多边形
  2. // DrawPolygon
  3. /*
  4. 绘制面
  5. */
  6. class DrawPolygon {
  7. constructor(arg) {
  8. this.viewer = arg.viewer;
  9. this.Cesium = arg.Cesium;
  10. // this.callback=arg.callback;
  11. this._polygon = null; //活动面
  12. this._polygonLast = null; //最后一个面
  13. this._positions = []; //活动点
  14. this._entities_point = []; //脏数据
  15. this._entities_polygon = []; //脏数据
  16. this._polygonData = null; //用户构造面
  17. this.DrawStartEvent = new Cesium.Event(); //开始绘制事件
  18. this.DrawEndEvent = new Cesium.Event(); //结束绘制事件
  19. /* 通用参数集合 */
  20. this._param = {
  21. id: "DrawStraightArrow",
  22. polygonColor: 'rgba(0,255,0,0.5)', //面填充颜色
  23. outlineColor: 'rgba(255, 255, 255, 1)', //边框颜色
  24. outlineWidth: 1, //边框宽度
  25. }
  26. /* 创建面材质 */
  27. this.polygonMaterial = Cesium.Color.fromCssColorString(this._param.polygonColor);
  28. /* 创建线材质 */
  29. // this.outlineMaterial = new Cesium.PolylineDashMaterialProperty({//曲线
  30. // dashLength: 16,
  31. // color: Cesium.Color.fromCssColorString(this._param.outlineColor)
  32. // });
  33. this.outlineMaterial = Cesium.Color.fromCssColorString(this._param.outlineColor);
  34. }
  35. //返回最后活动面
  36. get polygon() {
  37. return this._polygonLast;
  38. }
  39. //返回面数据用于加载面
  40. getData() {
  41. return this._polygonData;
  42. }
  43. // 修改编辑调用计算
  44. computePosition(data){
  45. //计算面
  46. let $this = this
  47. var lnglatArr = [];
  48. for (var i = 0; i < data.length; i++) {
  49. var lnglat = $this.cartesianToLatlng(data[i]);
  50. lnglatArr.push(lnglat)
  51. }
  52. $this._polygonData = lnglatArr;
  53. return new $this.Cesium.PolygonHierarchy(data);
  54. }
  55. //加载面
  56. addload(data) {
  57. var $this = this;
  58. var lnglatArr = [];
  59. for (var i = 0; i < data.length; i++) {
  60. var lnglat = $this.LatlngTocartesian(data[i]);
  61. lnglatArr.push(lnglat)
  62. }
  63. return this.viewer.entities.add({
  64. Type: 'DrawPolygon',
  65. Position: data,
  66. id:data.id || $this.objId,
  67. polygon: {
  68. hierarchy: new $this.Cesium.PolygonHierarchy(lnglatArr),
  69. clampToGround: true,
  70. show: true,
  71. fill: true,
  72. material: $this.Cesium.Color.RED.withAlpha(0.9),
  73. width: 3,
  74. outlineColor: $this.Cesium.Color.BLACK,
  75. outlineWidth: 1,
  76. outline: false
  77. }
  78. });
  79. }
  80. //开始绘制
  81. startCreate(drawType) {
  82. this.drawType = drawType;
  83. var $this = this;
  84. this.handler = new this.Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
  85. this.handler.setInputAction(function (evt) { //单机开始绘制
  86. var cartesian = $this.getCatesian3FromPX(evt.position);
  87. if ($this._positions.length == 0) {
  88. $this._positions.push(cartesian.clone());
  89. }
  90. $this.createPoint(cartesian);
  91. $this._positions.push(cartesian);
  92. }, $this.Cesium.ScreenSpaceEventType.LEFT_CLICK);
  93. this.handler.setInputAction(function (evt) { //移动时绘制面
  94. if ($this._positions.length < 1) return;
  95. var cartesian = $this.getCatesian3FromPX(evt.endPosition);
  96. if ($this._positions.length == 3) {
  97. if (!$this.Cesium.defined($this._polygon)) {
  98. $this._polygon = $this.createPolygon();
  99. }
  100. }
  101. $this._positions.pop();
  102. $this._positions.push(cartesian);
  103. }, $this.Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  104. this.handler.setInputAction(function (evt) {
  105. if (!$this._polygon) return;
  106. var cartesian = $this.getCatesian3FromPX(evt.position);
  107. $this._positions.pop();
  108. // $this._positions.push(cartesian);
  109. $this.createPoint(cartesian);
  110. $this._polygonData = $this._positions.concat();
  111. $this.viewer.entities.remove($this._positions); //移除
  112. $this._positions = null;
  113. $this._positions = [];
  114. var lnglatArr = [];
  115. for (var i = 0; i < $this._polygonData.length; i++) {
  116. var lnglat = $this.cartesianToLatlng($this._polygonData[i]);
  117. lnglatArr.push(lnglat)
  118. }
  119. $this._polygonData = lnglatArr;
  120. var Polygon = $this.addload($this._polygonData);
  121. $this._entities_polygon.push(Polygon);
  122. $this._polygonLast = Polygon;
  123. // if(typeof $this.callback=="function"){
  124. // $this.callback(Polygon);
  125. // }
  126. $this.clearPoint();
  127. $this.destroy()
  128. }, $this.Cesium.ScreenSpaceEventType.RIGHT_CLICK);
  129. }
  130. //创建面
  131. createPolygon() {
  132. var $this = this;
  133. var polygon = this.viewer.entities.add({
  134. polygon: {
  135. hierarchy: new $this.Cesium.CallbackProperty(function () {
  136. return new $this.Cesium.PolygonHierarchy($this._positions);
  137. }, false),
  138. clampToGround: true,
  139. show: true,
  140. fill: true,
  141. material: $this.Cesium.Color.RED.withAlpha(0.5),
  142. width: 3,
  143. outlineColor: $this.Cesium.Color.BLACK,
  144. outlineWidth: 1,
  145. outline: false
  146. }
  147. });
  148. $this._entities_polygon.push(polygon);
  149. return polygon;
  150. }
  151. cartesianToLatlng(cartesian) {
  152. var latlng = this.viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian);
  153. var lat = this.Cesium.Math.toDegrees(latlng.latitude);
  154. var lng = this.Cesium.Math.toDegrees(latlng.longitude);
  155. return [lng, lat];
  156. }
  157. LatlngTocartesian(latlng) {
  158. let cartesian3 = this.Cesium.Cartesian3.fromDegrees(latlng[0], latlng[1]);
  159. return cartesian3
  160. }
  161. clearPoint() {
  162. this.DrawEndEvent.raiseEvent(this._polygonLast, this._polygonData, this.drawType);
  163. for (var i = 0; i < this._entities_point.length; i++) {
  164. this.viewer.entities.remove(this._entities_point[i]);
  165. }
  166. this._entities_point = []; //脏数据
  167. }
  168. //创建点
  169. createPoint(cartesian) {
  170. var $this = this;
  171. var point = this.viewer.entities.add({
  172. position: cartesian,
  173. point: {
  174. pixelSize: 10,
  175. color: $this.Cesium.Color.RED,
  176. heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
  177. }
  178. });
  179. $this._entities_point.push(point);
  180. return point;
  181. }
  182. //销毁事件
  183. destroy() {
  184. if (this.handler) {
  185. this.handler.destroy();
  186. this.handler = null;
  187. }
  188. }
  189. //清空实体对象
  190. clear() {
  191. for (var i = 0; i < this._entities_point.length; i++) {
  192. this.viewer.entities.remove(this._entities_point[i]);
  193. }
  194. for (var i = 0; i < this._entities_polygon.length; i++) {
  195. this.viewer.entities.remove(this._entities_polygon[i]);
  196. }
  197. this._polygon = null; //活动面
  198. this._polygonLast = null; //最后一个面
  199. this._positions = []; //活动点
  200. this._entities_point = []; //脏数据
  201. this._entities_polygon = []; //脏数据
  202. this._polygonData = null; //用户构造面
  203. }
  204. getCatesian3FromPX(px) {
  205. var cartesian;
  206. var ray = this.viewer.camera.getPickRay(px);
  207. if (!ray) return null;
  208. cartesian = this.viewer.scene.globe.pick(ray, this.viewer.scene);
  209. return cartesian;
  210. }
  211. }
  212. export default DrawPolygon