1
1
import ol from 'openlayers' ;
2
+ import '../core/MapExtend' ;
2
3
import {
3
4
Util
4
5
} from '../core/Util' ;
@@ -13,7 +14,12 @@ import {
13
14
* @class ol.source.Graphic
14
15
* @category Visualization Graphic
15
16
* @classdesc 高效率点图层源。
16
- * @param options -{Object} 图形参数
17
+ * @param options -{Object} 图形参数。如:<br>
18
+ * graphics - {ol.Graphic} 高效率点图层点要素。<br>
19
+ * map - [ol.Map]{@linkdoc -openlayers/ol.Map.html} openlayers 面对象。<br>
20
+ * isHighLight - {boolean} 事件响应是否支持要素高亮。默认为 true,即默认支持高亮。<br>
21
+ * highLightStyle - [ol.style]{@linkdoc -openlayers/ol.style.html} 高亮风格,默认为 defaultHighLightStyle。<br>
22
+ * onClick - {function} 点击事件方法。将在下个版本弃用。<br>
17
23
* @extends ol.source.ImageCanvas{@linkdoc-openlayers/ol.source.ImageCanvas }
18
24
*/
19
25
export class Graphic extends ol . source . ImageCanvas {
@@ -31,16 +37,20 @@ export class Graphic extends ol.source.ImageCanvas {
31
37
this . graphics_ = options . graphics ;
32
38
this . map = options . map ;
33
39
this . highLightStyle = options . highLightStyle ;
40
+ //是否支持高亮,默认支持
41
+ this . isHighLight = typeof options . isHighLight === "undefined" ? true : options . isHighLight ;
34
42
this . hitGraphicLayer = null ;
35
-
43
+ this . _forEachFeatureAtCoordinate = _forEachFeatureAtCoordinate ;
36
44
37
45
var me = this ;
46
+
47
+ //todo 将被弃用
38
48
if ( options . onClick ) {
39
49
me . map . on ( 'click' , function ( e ) {
40
50
var coordinate = e . coordinate ;
41
51
var resolution = e . frameState . viewState . resolution ;
42
52
var pixel = e . pixel ;
43
- me . forEachFeatureAtCoordinate ( coordinate , resolution , options . onClick , pixel ) ;
53
+ me . _forEachFeatureAtCoordinate ( coordinate , resolution , options . onClick , pixel ) ;
44
54
} ) ;
45
55
}
46
56
@@ -118,6 +128,115 @@ export class Graphic extends ol.source.ImageCanvas {
118
128
var y = ( pixelP [ 1 ] - center [ 1 ] ) * scaleRatio + center [ 1 ] ;
119
129
return [ x , y ] ;
120
130
}
131
+
132
+ /**
133
+ * @private
134
+ * @function ol.source.Graphic.prototype._forEachFeatureAtCoordinate
135
+ * @description 获取在视图上的要素
136
+ * @param coordinate -{string} 坐标
137
+ * @param resolution -{number} 分辨率
138
+ * @param callback -{function} 回调函数
139
+ */
140
+ function _forEachFeatureAtCoordinate ( coordinate , resolution , callback , evtPixel ) {
141
+ let graphics = me . getGraphicsInExtent ( ) ;
142
+ for ( let i = graphics . length - 1 ; i >= 0 ; i -- ) {
143
+ //已经被高亮的graphics 不被选选中
144
+ if ( graphics [ i ] . getStyle ( ) instanceof HitCloverShape ) {
145
+ continue ;
146
+ }
147
+ let center = graphics [ i ] . getGeometry ( ) . getCoordinates ( ) ;
148
+ let image = new ol . style . Style ( {
149
+ image : graphics [ i ] . getStyle ( )
150
+ } ) . getImage ( ) ;
151
+ let extent = [ ] ;
152
+ extent [ 0 ] = center [ 0 ] - image . getAnchor ( ) [ 0 ] * resolution ;
153
+ extent [ 2 ] = center [ 0 ] + image . getAnchor ( ) [ 0 ] * resolution ;
154
+ extent [ 1 ] = center [ 1 ] - image . getAnchor ( ) [ 1 ] * resolution ;
155
+ extent [ 3 ] = center [ 1 ] + image . getAnchor ( ) [ 1 ] * resolution ;
156
+ if ( ol . extent . containsCoordinate ( extent , coordinate ) ) {
157
+ if ( me . isHighLight ) {
158
+ me . _highLight ( center , image , graphics [ i ] , evtPixel ) ;
159
+ }
160
+ return callback ( graphics [ i ] ) ;
161
+ }
162
+ if ( me . isHighLight ) {
163
+ me . _highLightClose ( ) ;
164
+ }
165
+ }
166
+ return undefined ;
167
+ }
168
+
169
+ }
170
+
171
+ /**
172
+ * @function ol.source.Graphic.prototype._highLightClose
173
+ * @description 关闭高亮要素显示
174
+ * @private
175
+ */
176
+ _highLightClose ( ) {
177
+ this . selected = null ;
178
+ if ( this . hitGraphicLayer ) {
179
+ this . map . removeLayer ( this . hitGraphicLayer ) ;
180
+ this . hitGraphicLayer = null ;
181
+ }
182
+ this . changed ( ) ;
183
+ }
184
+
185
+ /**
186
+ * @function ol.source.Graphic.prototype._highLight
187
+ * @description 高亮显示选中要素
188
+ * @param selectGraphic - {ol.Graphic} 高效率点图层点要素
189
+ * @param evtPixel - [ol.Pixel]{@linkdoc -openlayers/ol.html#.Pixel} 当前选中的屏幕像素坐标
190
+ * @private
191
+ */
192
+ _highLight ( center , image , selectGraphic , evtPixel ) {
193
+ if ( selectGraphic . getStyle ( ) instanceof CloverShape ) {
194
+ if ( this . hitGraphicLayer ) {
195
+ this . map . removeLayer ( this . hitGraphicLayer ) ;
196
+ this . hitGraphicLayer = null ;
197
+ }
198
+ var pixel = this . map . getPixelFromCoordinate ( [ center [ 0 ] , center [ 1 ] ] ) ;
199
+ //点击点与中心点的角度
200
+ evtPixel = evtPixel || [ 0 , 0 ] ;
201
+ var angle = ( Math . atan2 ( evtPixel [ 1 ] - pixel [ 1 ] , evtPixel [ 0 ] - pixel [ 0 ] ) ) / Math . PI * 180 ;
202
+ angle = angle > 0 ? angle : 360 + angle ;
203
+ //确定扇叶
204
+ var index = Math . ceil ( angle / ( image . getAngle ( ) + image . getSpaceAngle ( ) ) ) ;
205
+ //扇叶的起始角度
206
+ var sAngle = ( index - 1 ) * ( image . getAngle ( ) + image . getSpaceAngle ( ) ) ;
207
+ //渲染参数
208
+ var opts = {
209
+ stroke : new ol . style . Stroke ( {
210
+ color : "#ff0000" ,
211
+ width : 1
212
+ } ) ,
213
+ fill : new ol . style . Fill ( {
214
+ color : "#0099ff"
215
+ } ) ,
216
+ radius : image . getRadius ( ) ,
217
+ angle : image . getAngle ( ) ,
218
+ eAngle : sAngle + image . getAngle ( ) ,
219
+ sAngle : sAngle
220
+ } ;
221
+ if ( this . highLightStyle && this . highLightStyle instanceof HitCloverShape ) {
222
+ opts . stroke = this . highLightStyle . getStroke ( ) ;
223
+ opts . fill = this . highLightStyle . getFill ( ) ;
224
+ opts . radius = this . highLightStyle . getRadius ( ) ;
225
+ opts . angle = this . highLightStyle . getAngle ( ) ;
226
+ }
227
+ var hitGraphic = new ol . Graphic ( new ol . geom . Point ( center ) ) ;
228
+ hitGraphic . setStyle ( new HitCloverShape ( opts ) ) ;
229
+ this . hitGraphicLayer = new ol . layer . Image ( {
230
+ source : new ol . source . Graphic ( {
231
+ map : this . map ,
232
+ graphics : [ hitGraphic ]
233
+ } )
234
+ } ) ;
235
+ this . map . addLayer ( this . hitGraphicLayer ) ;
236
+ } else {
237
+ this . selected = selectGraphic ;
238
+ this . changed ( ) ;
239
+ }
121
240
}
122
241
123
242
/**
@@ -143,86 +262,6 @@ export class Graphic extends ol.source.ImageCanvas {
143
262
return graphics ;
144
263
}
145
264
146
- /**
147
- * @private
148
- * @function ol.source.Graphic.prototype.forEachFeatureAtCoordinate
149
- * @description 获取在视图上的要素
150
- * @param coordinate -{string} 坐标
151
- * @param resolution -{number} 分辨率
152
- * @param callback -{function} 回调函数
153
- */
154
- forEachFeatureAtCoordinate ( coordinate , resolution , callback , evtPixel ) {
155
- var graphics = this . getGraphicsInExtent ( ) ;
156
- for ( var i = graphics . length - 1 ; i > 0 ; i -- ) {
157
- var center = graphics [ i ] . getGeometry ( ) . getCoordinates ( ) ;
158
- var image = new ol . style . Style ( {
159
- image : graphics [ i ] . getStyle ( )
160
- } ) . getImage ( ) ;
161
- var extent = [ ] ;
162
- extent [ 0 ] = center [ 0 ] - image . getAnchor ( ) [ 0 ] * resolution ;
163
- extent [ 2 ] = center [ 0 ] + image . getAnchor ( ) [ 0 ] * resolution ;
164
- extent [ 1 ] = center [ 1 ] - image . getAnchor ( ) [ 1 ] * resolution ;
165
- extent [ 3 ] = center [ 1 ] + image . getAnchor ( ) [ 1 ] * resolution ;
166
- if ( ol . extent . containsCoordinate ( extent , coordinate ) ) {
167
- if ( graphics [ i ] . getStyle ( ) instanceof CloverShape ) {
168
- if ( this . hitGraphicLayer ) {
169
- this . map . removeLayer ( this . hitGraphicLayer ) ;
170
- this . hitGraphicLayer = null ;
171
- }
172
- var pixel = this . map . getPixelFromCoordinate ( [ center [ 0 ] , center [ 1 ] ] ) ;
173
- //点击点与中心点的角度
174
- evtPixel = evtPixel || [ 0 , 0 ] ;
175
- var angle = ( Math . atan2 ( evtPixel [ 1 ] - pixel [ 1 ] , evtPixel [ 0 ] - pixel [ 0 ] ) ) / Math . PI * 180 ;
176
- angle = angle > 0 ? angle : 360 + angle ;
177
- //确定扇叶
178
- var index = Math . ceil ( angle / ( image . getAngle ( ) + image . getSpaceAngle ( ) ) ) ;
179
- //扇叶的起始角度
180
- var sAngle = ( index - 1 ) * ( image . getAngle ( ) + image . getSpaceAngle ( ) ) ;
181
- //渲染参数
182
- var opts = {
183
- stroke : new ol . style . Stroke ( {
184
- color : "#ff0000" ,
185
- width : 1
186
- } ) ,
187
- fill : new ol . style . Fill ( {
188
- color : "#0099ff"
189
- } ) ,
190
- radius : image . getRadius ( ) ,
191
- angle : image . getAngle ( ) ,
192
- eAngle : sAngle + image . getAngle ( ) ,
193
- sAngle : sAngle
194
- } ;
195
- if ( this . highLightStyle && this . highLightStyle instanceof HitCloverShape ) {
196
- opts . stroke = this . highLightStyle . getStroke ( ) ;
197
- opts . fill = this . highLightStyle . getFill ( ) ;
198
- opts . radius = this . highLightStyle . getRadius ( ) ;
199
- opts . angle = this . highLightStyle . getAngle ( ) ;
200
- }
201
- var hitGraphic = new ol . Graphic ( new ol . geom . Point ( center ) ) ;
202
- hitGraphic . setStyle ( new HitCloverShape ( opts ) ) ;
203
- this . hitGraphicLayer = new ol . layer . Image ( {
204
- source : new ol . source . Graphic ( {
205
- map : this . map ,
206
- graphics : [ hitGraphic ]
207
- } )
208
- } ) ;
209
- this . map . addLayer ( this . hitGraphicLayer ) ;
210
- } else {
211
- this . selected = graphics [ i ] ;
212
- this . changed ( ) ;
213
- }
214
- callback ( graphics [ i ] ) ;
215
- return ;
216
- }
217
- this . selected = null ;
218
- if ( this . hitGraphicLayer ) {
219
- this . map . removeLayer ( this . hitGraphicLayer ) ;
220
- this . hitGraphicLayer = null ;
221
- }
222
- this . changed ( ) ;
223
- }
224
- callback ( ) ;
225
- }
226
265
}
227
266
228
267
ol . source . Graphic = Graphic ;
0 commit comments