Skip to content

Commit 7d054f1

Browse files
committed
[update] 优化graphic 点击选择计算不准确的问题
1 parent 5948ede commit 7d054f1

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

src/openlayers/overlay/Graphic.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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) {
@@ -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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy