@@ -87,7 +87,7 @@ export class Graphic extends ol.source.ImageCanvas {
8787
8888 if ( options . onClick ) {
8989 me . map . on ( 'click' , function ( e ) {
90- me . map . forEachFeatureAtPixel ( e . pixel , options . onClick , { } , e ) ;
90+ me . map . forEachFeatureAtPixel ( e . pixel , options . onClick , { } , e ) ;
9191 } ) ;
9292 }
9393
@@ -151,6 +151,7 @@ export class Graphic extends ol.source.ImageCanvas {
151151 */
152152 function _forEachFeatureAtCoordinate ( coordinate , resolution , callback , evtPixel , e ) {
153153 let graphics = me . getGraphicsInExtent ( ) ;
154+ let includeGraphics = [ ] ; // 点密集的时候,符合条件的有多个 还需精确计算
154155 for ( let i = graphics . length - 1 ; i >= 0 ; i -- ) {
155156 let style = graphics [ i ] . getStyle ( ) ;
156157 if ( ! style ) {
@@ -182,7 +183,7 @@ export class Graphic extends ol.source.ImageCanvas {
182183 for ( let index = 0 ; index < 8 ; index ++ ) {
183184 const radian = ( ratation + index * perAngle ) / 180 * Math . PI ;
184185 coors . push ( [ center [ 0 ] + r * Math . cos ( radian ) ,
185- center [ 1 ] - r * Math . sin ( radian )
186+ center [ 1 ] - r * Math . sin ( radian )
186187 ] ) ;
187188 }
188189 coors . push ( center ) ;
@@ -201,7 +202,8 @@ export class Graphic extends ol.source.ImageCanvas {
201202 extent [ 1 ] = center [ 1 ] - image . getAnchor ( ) [ 1 ] * resolution ;
202203 extent [ 3 ] = center [ 1 ] + image . getAnchor ( ) [ 1 ] * resolution ;
203204 if ( ol . extent . containsCoordinate ( extent , coordinate ) ) {
204- contain = true ;
205+ includeGraphics . push ( graphics [ i ] ) ;
206+ // contain = true;
205207 }
206208 }
207209
@@ -218,11 +220,60 @@ export class Graphic extends ol.source.ImageCanvas {
218220 me . _highLightClose ( ) ;
219221 }
220222 }
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+ }
221243 return undefined ;
222244 }
223245
224246 }
225247
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+
226277 /**
227278 * @function ol.source.Graphic.prototype.setGraphics
228279 * @description 设置绘制的点要素,会覆盖之前的所有要素。
0 commit comments