Skip to content

[update] 优化graphic 点击选择计算不准确的问题 #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions src/openlayers/overlay/Graphic.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class Graphic extends ol.source.ImageCanvas {

if (options.onClick) {
me.map.on('click', function (e) {
me.map.forEachFeatureAtPixel(e.pixel, options.onClick,{},e);
me.map.forEachFeatureAtPixel(e.pixel, options.onClick, {}, e);
});
}

Expand Down Expand Up @@ -151,6 +151,7 @@ export class Graphic extends ol.source.ImageCanvas {
*/
function _forEachFeatureAtCoordinate(coordinate, resolution, callback, evtPixel, e) {
let graphics = me.getGraphicsInExtent();
let includeGraphics = []; // 点密集的时候,符合条件的有多个 还需精确计算
for (let i = graphics.length - 1; i >= 0; i--) {
let style = graphics[i].getStyle();
if (!style) {
Expand Down Expand Up @@ -182,7 +183,7 @@ export class Graphic extends ol.source.ImageCanvas {
for (let index = 0; index < 8; index++) {
const radian = (ratation + index * perAngle) / 180 * Math.PI;
coors.push([center[0] + r * Math.cos(radian),
center[1] - r * Math.sin(radian)
center[1] - r * Math.sin(radian)
]);
}
coors.push(center);
Expand All @@ -201,7 +202,8 @@ export class Graphic extends ol.source.ImageCanvas {
extent[1] = center[1] - image.getAnchor()[1] * resolution;
extent[3] = center[1] + image.getAnchor()[1] * resolution;
if (ol.extent.containsCoordinate(extent, coordinate)) {
contain = true;
includeGraphics.push(graphics[i]);
// contain = true;
}
}

Expand All @@ -218,11 +220,60 @@ export class Graphic extends ol.source.ImageCanvas {
me._highLightClose();
}
}
// 精确计算
let exactGraphic = this._getExactGraphic(includeGraphics, evtPixel);
if (exactGraphic) {
let _style = exactGraphic.getStyle(),
_center = exactGraphic.getGeometry().getCoordinates(),
_image = new ol.style.Style({
image: _style
}).getImage();

if (me.isHighLight) {
me._highLight(_center, _image, exactGraphic, evtPixel);
}
if (callback) {
callback(exactGraphic, e);
}
} else {
if (me.isHighLight) {
me._highLightClose();
}
}
return undefined;
}

}

/**
* @private
* @function ol.source.Graphic.prototype._getExactGraphic
* @description 获取到精确的graphic。
* @param {Array.<ol.Graphic>} graphics - 点要素对象数组。
* @param {ol.Pixel} evtPixel - 当前选中的屏幕像素坐标。
*/
_getExactGraphic(graphics, evtPixel) {
if (graphics.length === 0) {
return false;
} else if (graphics.length === 1) {
return graphics[0];
} else {
let distances = [];
graphics.map((graphic, index) => {
let center = graphic.getGeometry().getCoordinates(),
centerPixel = this.map.getPixelFromCoordinate(center),
distance = Math.sqrt(Math.pow((centerPixel[0] - evtPixel[0]), 2) + Math.pow((centerPixel[1] - evtPixel[1]), 2));
distances.push({ distance: distance, index: index });
return null;
});

distances.sort((a, b) => {
return a.distance - b.distance
});
return graphics[distances[0].index];
}
}

/**
* @function ol.source.Graphic.prototype.setGraphics
* @description 设置绘制的点要素,会覆盖之前的所有要素。
Expand Down
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