Skip to content

Commit bc53057

Browse files
committed
【fix】修复ol 高效率点图层click事件触发多次的问题 review by xiongjj
1 parent c593c35 commit bc53057

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

src/openlayers/overlay/Graphic.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { CloverShape } from './graphic/CloverShape';
88
import { CommonUtil } from '@supermap/iclient-common';
99
import { GraphicWebGLRenderer } from './graphic/WebGLRenderer';
1010
import { GraphicCanvasRenderer } from './graphic/CanvasRenderer';
11-
import {Graphic as OverlayGraphic} from './graphic/Graphic';
11+
import { Graphic as OverlayGraphic } from './graphic/Graphic';
1212
import ImageCanvasSource from 'ol/source/ImageCanvas';
1313
import Style from 'ol/style/Style';
1414
import CircleStyle from 'ol/style/Circle';
@@ -82,12 +82,29 @@ export class Graphic extends ImageCanvasSource {
8282
const me = this;
8383

8484
if (options.onClick) {
85-
me.map.on('click', function (e) {
86-
me.map.forEachFeatureAtPixel(e.pixel, options.onClick, {}, e);
85+
me.map.on('click', function(e) {
86+
const features = me.map.getFeaturesAtPixel(e.pixel) || [];
87+
for (let index = 0; index < features.length; index++) {
88+
const graphic = features[index];
89+
if (me.graphics.indexOf(graphic) > -1) {
90+
options.onClick(graphic, e);
91+
if (me.isHighLight) {
92+
me._highLight(
93+
graphic.getGeometry().getCoordinates(),
94+
new Style({
95+
image: graphic.getStyle()
96+
}).getImage(),
97+
graphic,
98+
e.pixel
99+
);
100+
}
101+
}
102+
break;
103+
}
87104
});
88105
}
89-
90-
function canvasFunctionInternal_(extent, resolution, pixelRatio, size, projection) { // eslint-disable-line no-unused-vars
106+
//eslint-disable-next-line no-unused-vars
107+
function canvasFunctionInternal_(extent, resolution, pixelRatio, size, projection) {
91108
if (!me.renderer) {
92109
me.renderer = createRenderer(size, pixelRatio);
93110
}
@@ -122,10 +139,10 @@ export class Graphic extends ImageCanvasSource {
122139
opt = CommonUtil.extend(me, opt);
123140
opt.pixelRatio = pixelRatio;
124141
opt.container = me.map.getViewport().getElementsByClassName('ol-overlaycontainer')[0];
125-
opt.onBeforeRender = function () {
142+
opt.onBeforeRender = function() {
126143
return false;
127144
};
128-
opt.onAfterRender = function () {
145+
opt.onAfterRender = function() {
129146
return false;
130147
};
131148

@@ -197,9 +214,6 @@ export class Graphic extends ImageCanvasSource {
197214
}
198215

199216
if (contain === true) {
200-
if (me.isHighLight) {
201-
me._highLight(center, image, graphics[i], evtPixel);
202-
}
203217
if (callback) {
204218
callback(graphics[i], e);
205219
}
@@ -522,13 +536,13 @@ export class Graphic extends ImageCanvasSource {
522536
getGraphicsInExtent(extent) {
523537
var graphics = [];
524538
if (!extent) {
525-
this.graphics.map(function (graphic) {
539+
this.graphics.map(function(graphic) {
526540
graphics.push(graphic);
527541
return graphic;
528542
});
529543
return graphics;
530544
}
531-
this.graphics.map(function (graphic) {
545+
this.graphics.map(function(graphic) {
532546
if (olExtent.containsExtent(extent, graphic.getGeometry().getExtent())) {
533547
graphics.push(graphic);
534548
}

0 commit comments

Comments
 (0)