Skip to content

Commit 6428062

Browse files
committed
add map.identifyAtPoint and vectorLayer.identifyAtPoint
1 parent 3f4b9f1 commit 6428062

File tree

5 files changed

+83
-20
lines changed

5 files changed

+83
-20
lines changed

src/core/util/style.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ export function getGradientStamp(g) {
3333
return keys.join('_');
3434
}
3535

36+
// back-compatibility alias
37+
export function getSymbolStamp(symbol, prefix) {
38+
return getSymbolHash(symbol, prefix);
39+
}
40+
3641
/**
3742
* Get stamp of a symbol
3843
* @param {Object|Object[]} symbol symbol

src/layer/VectorLayer.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import OverlayLayer from './OverlayLayer';
66
import Painter from '../renderer/geometry/Painter';
77
import CollectionPainter from '../renderer/geometry/CollectionPainter';
88
import Coordinate from '../geo/Coordinate';
9+
import Point from '../geo/Point';
910
import { LineString, Curve } from '../geometry';
1011
import PointExtent from '../geo/PointExtent';
1112

@@ -81,23 +82,41 @@ class VectorLayer extends OverlayLayer {
8182
*/
8283
identify(coordinate, options = {}) {
8384
const renderer = this.getRenderer();
84-
// only iterate drawn geometries when onlyVisible is true.
85-
if (options['onlyVisible'] && renderer && renderer.identify) {
86-
return renderer.identify(coordinate, options);
87-
}
8885
if (!(coordinate instanceof Coordinate)) {
8986
coordinate = new Coordinate(coordinate);
9087
}
91-
return this._hitGeos(this._geoList, coordinate, options);
88+
const cp = this.getMap().coordToContainerPoint(coordinate);
89+
// only iterate drawn geometries when onlyVisible is true.
90+
if (options['onlyVisible'] && renderer && renderer.identifyAtPoint) {
91+
return renderer.identifyAtPoint(cp, options);
92+
}
93+
return this._hitGeos(this._geoList, cp, options);
94+
}
95+
96+
/**
97+
* Identify the geometries on the given container point
98+
* @param {maptalks.Point} point - container point to identify
99+
* @param {Object} [options=null] - options
100+
* @param {Object} [options.tolerance=0] - identify tolerance in pixel
101+
* @param {Object} [options.count=null] - result count
102+
* @return {Geometry[]} geometries identified
103+
*/
104+
identifyAtPoint(point, options = {}) {
105+
const renderer = this.getRenderer();
106+
if (!(point instanceof Point)) {
107+
point = new Point(point);
108+
}
109+
// only iterate drawn geometries when onlyVisible is true.
110+
if (options['onlyVisible'] && renderer && renderer.identifyAtPoint) {
111+
return renderer.identifyAtPoint(point, options);
112+
}
113+
return this._hitGeos(this._geoList, point, options);
92114
}
93115

94-
_hitGeos(geometries, coordinate, options = {}) {
116+
_hitGeos(geometries, cp, options = {}) {
95117
const filter = options['filter'],
96118
tolerance = options['tolerance'],
97119
hits = [];
98-
const map = this.getMap();
99-
const point = map.coordToPoint(coordinate);
100-
const cp = map._pointToContainerPoint(point, undefined, 0, point);
101120
for (let i = geometries.length - 1; i >= 0; i--) {
102121
const geo = geometries[i];
103122
if (!geo || !geo.isVisible() || !geo._getPainter() || !geo.options['interactive']) {

src/map/Map.Topo.js

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { INTERNAL_LAYER_PREFIX } from '../core/Constants';
22
import { extend, isString, isArrayHasData, pushIn } from '../core/util';
33
import Coordinate from '../geo/Coordinate';
4+
import Point from '../geo/Point';
45
import Map from './Map';
56

67
/**
@@ -67,9 +68,48 @@ Map.include(/** @lends Map.prototype */ {
6768
* });
6869
*/
6970
identify: function (opts, callback) {
70-
if (!opts) {
71-
return this;
72-
}
71+
opts = opts || {};
72+
const coordinate = new Coordinate(opts['coordinate']);
73+
return this._identify(opts, callback, layer => layer.identify(coordinate, opts));
74+
},
75+
76+
/**
77+
* Identify the geometries on the given container point.
78+
* @param {Object} opts - the identify options
79+
* @param {Point} opts.containerPoint - container point to identify
80+
* @param {Object} opts.layers - the layers to perform identify on.
81+
* @param {Function} [opts.filter=null] - filter function of the result geometries, return false to exclude.
82+
* @param {Number} [opts.count=null] - limit of the result count.
83+
* @param {Number} [opts.tolerance=0] - identify tolerance in pixel.
84+
* @param {Boolean} [opts.includeInternals=false] - whether to identify internal layers.
85+
* @param {Boolean} [opts.includeInvisible=false] - whether to identify invisible layers.
86+
* @param {Function} callback - the callback function using the result geometries as the parameter.
87+
* @return {Map} this
88+
* @example
89+
* map.identifyAtPoint({
90+
* containerPoint: [200, 300],
91+
* layers: [layer]
92+
* },
93+
* geos => {
94+
* console.log(geos);
95+
* });
96+
*/
97+
identifyAtPoint: function (opts, callback) {
98+
opts = opts || {};
99+
const containerPoint = new Point(opts['containerPoint']);
100+
const coordinate = this.containerPointToCoord(containerPoint);
101+
return this._identify(opts, callback, layer => {
102+
if (layer.identifyAtPoint) {
103+
return layer.identifyAtPoint(containerPoint, opts);
104+
} else if (coordinate) {
105+
return layer.identify(coordinate, opts);
106+
} else {
107+
return [];
108+
}
109+
});
110+
},
111+
112+
_identify: function (opts, callback, fn) {
73113
const reqLayers = opts['layers'];
74114
if (!isArrayHasData(reqLayers)) {
75115
return this;
@@ -82,8 +122,7 @@ Map.include(/** @lends Map.prototype */ {
82122
layers.push(reqLayers[i]);
83123
}
84124
}
85-
const coordinate = new Coordinate(opts['coordinate']);
86-
const options = extend({}, opts);
125+
87126
const hits = [];
88127
for (let i = layers.length - 1; i >= 0; i--) {
89128
if (opts['count'] && hits.length >= opts['count']) {
@@ -93,7 +132,7 @@ Map.include(/** @lends Map.prototype */ {
93132
if (!layer || !layer.getMap() || (!opts['includeInvisible'] && !layer.isVisible()) || (!opts['includeInternals'] && layer.getId().indexOf(INTERNAL_LAYER_PREFIX) >= 0)) {
94133
continue;
95134
}
96-
const layerHits = layer.identify(coordinate, options);
135+
const layerHits = fn(layer);
97136
if (layerHits) {
98137
if (Array.isArray(layerHits)) {
99138
pushIn(hits, layerHits);

src/map/handler/Map.GeometryEvents.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class MapGeometryEventsHandler extends Handler {
228228
return true;
229229
},
230230
'count': 1,
231-
'coordinate': coordinate,
231+
'containerPoint': containerPoint,
232232
'onlyVisible' : map.options['onlyVisibleGeometryEvents'],
233233
'layers': layers
234234
};
@@ -239,10 +239,10 @@ class MapGeometryEventsHandler extends Handler {
239239
if (map.isInteracting()) {
240240
return;
241241
}
242-
map.identify(identifyOptions, callback);
242+
map.identifyAtPoint(identifyOptions, callback);
243243
});
244244
} else {
245-
map.identify(identifyOptions, callback);
245+
map.identifyAtPoint(identifyOptions, callback);
246246
}
247247

248248
function fireGeometryEvent(geometries) {

src/renderer/layer/vectorlayer/VectorLayerCanvasRenderer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,12 @@ class VectorLayerRenderer extends OverlayLayerCanvasRenderer {
209209
this._displayExtent = extent2D;
210210
}
211211

212-
identify(coordinate, options = {}) {
212+
identifyAtPoint(point, options = {}) {
213213
const geometries = this._geosToDraw;
214214
if (!geometries) {
215215
return [];
216216
}
217-
return this.layer._hitGeos(geometries, coordinate, options);
217+
return this.layer._hitGeos(geometries, point, options);
218218
}
219219

220220
_updateMapStateCache() {

0 commit comments

Comments
 (0)