@@ -87,7 +87,7 @@ export class Graphic extends ol.source.ImageCanvas {
87
87
88
88
if ( options . onClick ) {
89
89
me . map . on ( 'click' , function ( e ) {
90
- me . map . forEachFeatureAtPixel ( e . pixel , options . onClick , { } , e ) ;
90
+ me . map . forEachFeatureAtPixel ( e . pixel , options . onClick , { } , e ) ;
91
91
} ) ;
92
92
}
93
93
@@ -151,6 +151,7 @@ export class Graphic extends ol.source.ImageCanvas {
151
151
*/
152
152
function _forEachFeatureAtCoordinate ( coordinate , resolution , callback , evtPixel , e ) {
153
153
let graphics = me . getGraphicsInExtent ( ) ;
154
+ let includeGraphics = [ ] ; // 点密集的时候,符合条件的有多个 还需精确计算
154
155
for ( let i = graphics . length - 1 ; i >= 0 ; i -- ) {
155
156
let style = graphics [ i ] . getStyle ( ) ;
156
157
if ( ! style ) {
@@ -182,7 +183,7 @@ export class Graphic extends ol.source.ImageCanvas {
182
183
for ( let index = 0 ; index < 8 ; index ++ ) {
183
184
const radian = ( ratation + index * perAngle ) / 180 * Math . PI ;
184
185
coors . push ( [ center [ 0 ] + r * Math . cos ( radian ) ,
185
- center [ 1 ] - r * Math . sin ( radian )
186
+ center [ 1 ] - r * Math . sin ( radian )
186
187
] ) ;
187
188
}
188
189
coors . push ( center ) ;
@@ -201,7 +202,8 @@ export class Graphic extends ol.source.ImageCanvas {
201
202
extent [ 1 ] = center [ 1 ] - image . getAnchor ( ) [ 1 ] * resolution ;
202
203
extent [ 3 ] = center [ 1 ] + image . getAnchor ( ) [ 1 ] * resolution ;
203
204
if ( ol . extent . containsCoordinate ( extent , coordinate ) ) {
204
- contain = true ;
205
+ includeGraphics . push ( graphics [ i ] ) ;
206
+ // contain = true;
205
207
}
206
208
}
207
209
@@ -218,11 +220,60 @@ export class Graphic extends ol.source.ImageCanvas {
218
220
me . _highLightClose ( ) ;
219
221
}
220
222
}
223
+ // 精确计算
224
+ let exactGraphic = this . _getExactGraphic ( includeGraphics , evtPixel ) ;
225
+ if ( exactGraphic ) {
226
+ let _style = exactGraphic . getStyle ( ) ,
227
+ _center = exactGraphic . getGeometry ( ) . getCoordinates ( ) ,
228
+ _image = new ol . style . Style ( {
229
+ image : _style
230
+ } ) . getImage ( ) ;
231
+
232
+ if ( me . isHighLight ) {
233
+ me . _highLight ( _center , _image , exactGraphic , evtPixel ) ;
234
+ }
235
+ if ( callback ) {
236
+ callback ( exactGraphic , e ) ;
237
+ }
238
+ } else {
239
+ if ( me . isHighLight ) {
240
+ me . _highLightClose ( ) ;
241
+ }
242
+ }
221
243
return undefined ;
222
244
}
223
245
224
246
}
225
247
248
+ /**
249
+ * @private
250
+ * @function ol.source.Graphic.prototype._getExactGraphic
251
+ * @description 获取到精确的graphic。
252
+ * @param {Array.<ol.Graphic> } graphics - 点要素对象数组。
253
+ * @param {ol.Pixel } evtPixel - 当前选中的屏幕像素坐标。
254
+ */
255
+ _getExactGraphic ( graphics , evtPixel ) {
256
+ if ( graphics . length === 0 ) {
257
+ return false ;
258
+ } else if ( graphics . length === 1 ) {
259
+ return graphics [ 0 ] ;
260
+ } else {
261
+ let distances = [ ] ;
262
+ graphics . map ( ( graphic , index ) => {
263
+ let center = graphic . getGeometry ( ) . getCoordinates ( ) ,
264
+ centerPixel = this . map . getPixelFromCoordinate ( center ) ,
265
+ distance = Math . sqrt ( Math . pow ( ( centerPixel [ 0 ] - evtPixel [ 0 ] ) , 2 ) + Math . pow ( ( centerPixel [ 1 ] - evtPixel [ 1 ] ) , 2 ) ) ;
266
+ distances . push ( { distance : distance , index : index } ) ;
267
+ return null ;
268
+ } ) ;
269
+
270
+ distances . sort ( ( a , b ) => {
271
+ return a . distance - b . distance
272
+ } ) ;
273
+ return graphics [ distances [ 0 ] . index ] ;
274
+ }
275
+ }
276
+
226
277
/**
227
278
* @function ol.source.Graphic.prototype.setGraphics
228
279
* @description 设置绘制的点要素,会覆盖之前的所有要素。
0 commit comments