@@ -17,8 +17,6 @@ class PlotLayer3D extends Observable {
1717 super ( ) ;
1818 //viewer对象
1919 this . _viewer = viewer ;
20- //标绘图元id列表
21- this . _plotList = [ ] ;
2220 //primitive数组
2321 this . _primitiveCollection = new Cesium . PrimitiveCollection ( ) ;
2422 //图元可否编辑
@@ -40,7 +38,6 @@ class PlotLayer3D extends Observable {
4038 */
4139 addPlot ( plot ) {
4240 this . _primitiveCollection . add ( plot ) ;
43- this . _plotList . push ( plot . id ) ;
4441 return plot ;
4542 }
4643
@@ -55,33 +52,11 @@ class PlotLayer3D extends Observable {
5552 * @function module:PlotLayer3D.removePlotByID
5653 * @description 根据标绘图元ID删除标绘图元
5754 * @param id - {String} 必选项,要删除的标绘图元ID
58- * @return {Object } json
55+ * @return {Object } json,被删除的标绘图元
5956 */
6057 removePlotByID ( id ) {
61- //删除图元列表中的id
62- for ( let j = 0 ; j < this . _plotList . length ; j ++ ) {
63- if ( id === this . _plotList [ j ] ) {
64- this . _plotList . splice ( j , 1 ) ;
65- break ;
66- }
67- }
68-
69- return this . _removePlotByID ( id ) ;
70- }
71-
72- _removePlotByID ( id ) {
73- let scene = this . _getScene ( ) ;
74- let result ;
75-
76- //删除标绘图元
77- for ( let i = 0 ; i < scene . primitives . length ; i ++ ) {
78- if ( id === scene . primitives [ i ] . id ) {
79- result = scene . primitives . remove ( scene . primitives [ i ] ) ;
80- break ;
81- }
82- }
83-
84- return result ;
58+ let plot = this . getPlotByID ( id ) ;
59+ return this . removePlot ( plot ) ;
8560 }
8661
8762 /**
@@ -91,42 +66,45 @@ class PlotLayer3D extends Observable {
9166 * @return {Object } json
9267 */
9368 removePlot ( plot ) {
94- let scene = this . _getScene ( ) ;
95-
96- //删除图元列表中的id
97- for ( let i = 0 ; i < this . _plotList . length ; i ++ ) {
98- if ( id === this . _plotList [ i ] ) {
99- this . _plotList . splice ( i , 1 ) ;
100- break ;
101- }
69+ let plotLayer = this . _getPlotLayer ( ) ;
70+ if ( plotLayer ) {
71+ return plotLayer . remove ( plot ) ;
10272 }
103-
104- return scene . primitives . remove ( plot ) ;
10573 }
10674
10775 /**
10876 * @function module:PlotLayer3D.getPlotByID
10977 * @description 根据标绘图元ID获取标绘图元
11078 * @param id - {String} 必选项,标绘图元ID
111- * @return {Object } json
79+ * @return {Object } json,没找到返回undefined
11280 */
11381 getPlotByID ( id ) {
114- let index = this . _getPlotIndexById ( id ) ;
115- let scene = this . _getScene ( ) ;
82+ let plotLayer = this . _getPlotLayer ( ) ;
83+
84+ if ( ! plotLayer ) {
85+ return undefined ;
86+ }
11687
88+ let index = this . _getPlotIndexById ( id , plotLayer ) ;
89+ const { _primitives} = plotLayer ;
11790 if ( index !== undefined ) {
11891 return _primitives [ index ] ;
11992 }
12093 return undefined ;
12194 }
12295
123- _getPlotIndexById ( id ) {
124- let scene = this . _getScene ( ) ;
125-
96+ /**
97+ * @description 根据标绘图元ID获取标绘图元的index
98+ * @private
99+ *
100+ * @param id - {String} 必选项,标绘图元ID
101+ * @param plotLayer - {Object} 必选项,标绘图层
102+ * @return {Number } index
103+ */
104+ _getPlotIndexById ( id , plotLayer ) {
126105 let index = undefined ;
127106
128- const { _primitives} = scene . primitives ;
129-
107+ const { _primitives} = plotLayer ;
130108 for ( let i = 0 ; i < _primitives . length ; i ++ ) {
131109 if ( id === _primitives [ i ] . id ) {
132110 index = i ;
@@ -136,17 +114,35 @@ class PlotLayer3D extends Observable {
136114
137115 return index ;
138116 }
117+ /**
118+ * @description 根据标绘图层ID获取标绘图层
119+ * @private
120+ *
121+ * @return {Object } plotLayer 标绘图层,没找到返回undefined
122+ */
123+ _getPlotLayer ( ) {
124+ let scene = this . _getScene ( ) ;
125+ //根据图层id寻找标绘图层
126+ const { _primitives} = scene . primitives ;
127+ //标绘图层
128+ let primitiveCollection ;
129+ for ( let i = 0 ; i < _primitives . length ; i ++ ) {
130+ if ( this . _id === _primitives [ i ] . _id ) {
131+ primitiveCollection = _primitives [ i ] ;
132+ break ;
133+ }
134+ }
135+
136+ return primitiveCollection ;
137+ }
139138
140139 /**
141140 * @function module:PlotLayer3D.removeAll
142141 * @description 移除图层下的所有标绘图元
143142 */
144143 removeAll ( ) {
145- for ( let i = 0 ; i < this . _plotList . length ; i ++ ) {
146- this . _removePlotByID ( this . _plotList [ i ] ) ;
147- }
148-
149- this . _plotList = [ ] ;
144+ let plotLayer = this . _getPlotLayer ( ) ;
145+ plotLayer . removeAll ( ) ;
150146 }
151147
152148 /**
@@ -206,8 +202,6 @@ class PlotLayer3D extends Observable {
206202 primitive . fromGeoJSON ( geoFeature ) ;
207203 primitive . id = id + "_" + parseInt ( String ( Math . random ( ) * 1000000000 ) ) ;
208204
209- that . _plotList . push ( primitive . id ) ;
210-
211205 that . _addPrimitive ( primitive ) ;
212206 } ) ;
213207 }
0 commit comments