@antv/l7-component 2.23.1 → 2.23.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/es/interface.d.ts CHANGED
@@ -15,4 +15,15 @@ export interface IMarkerStyleOption {
15
15
  export interface IMarkerLayerOption {
16
16
  cluster: boolean;
17
17
  clusterOption: Partial<IMarkerStyleOption>;
18
+ /**
19
+ * Default marker options applied to markers added to this layer when not overridden per-marker.
20
+ * Example: { color: '#ff0000', style: { width: '24px', height: '24px' }, className: 'my-marker' }
21
+ */
22
+ markerOption?: Partial<{
23
+ color?: string;
24
+ style?: {
25
+ [key: string]: any;
26
+ };
27
+ className?: string;
28
+ }>;
18
29
  }
@@ -43,6 +43,7 @@ export default class MarkerLayer extends EventEmitter {
43
43
  addMarkers(): void;
44
44
  clear(): void;
45
45
  destroy(): void;
46
+ private updateMarkers;
46
47
  private addPoint;
47
48
  private removePoint;
48
49
  private initCluster;
@@ -56,6 +56,9 @@ export default class MarkerLayer extends EventEmitter {
56
56
  this.mapsService.on('camerachange', this.update); // amap1.x 更新事件
57
57
  this.mapsService.on('viewchange', this.update); // amap2.0 更新事件
58
58
  }
59
+ // 统一由 layer 管理 marker 的位置更新,避免每个 marker 单独注册地图事件
60
+ this.mapsService.on('camerachange', this.updateMarkers.bind(this));
61
+ this.mapsService.on('viewchange', this.updateMarkers.bind(this));
59
62
  this.mapsService.on('camerachange', this.setContainerSize.bind(this)); // amap1.x 更新事件
60
63
  this.mapsService.on('viewchange', this.setContainerSize.bind(this)); // amap2.0 更新事件
61
64
  this.addMarkers();
@@ -85,6 +88,54 @@ export default class MarkerLayer extends EventEmitter {
85
88
  addMarker(marker) {
86
89
  const cluster = this.markerLayerOption.cluster;
87
90
  marker.getMarkerLayerContainerSize = this.getContainerSize.bind(this);
91
+
92
+ // apply default markerOption if provided by layer
93
+ try {
94
+ const mOpt = this.markerLayerOption.markerOption;
95
+ if (mOpt && marker && typeof marker.getElement === 'function') {
96
+ const el = marker.getElement();
97
+ if (el) {
98
+ // apply className
99
+ if (mOpt.className) {
100
+ // DOM helper available
101
+ try {
102
+ DOM.addClass(el, mOpt.className);
103
+ } catch (e) {
104
+ el.className = `${el.className || ''} ${mOpt.className}`.trim();
105
+ }
106
+ }
107
+ // apply style properties
108
+ if (mOpt.style && typeof mOpt.style === 'object') {
109
+ Object.keys(mOpt.style).forEach(k => {
110
+ try {
111
+ // @ts-ignore
112
+ el.style[k] = mOpt.style[k];
113
+ } catch (e) {
114
+ // ignore invalid styles
115
+ }
116
+ });
117
+ }
118
+ // apply color: try find svg path inside marker element and set fill
119
+ if (mOpt.color) {
120
+ try {
121
+ // querySelector can return SVGPathElement; coerce to any for attribute access
122
+ const svgPath = el.querySelector ? el.querySelector('path') : null;
123
+ if (svgPath && typeof svgPath.setAttribute === 'function') {
124
+ svgPath.setAttribute('fill', mOpt.color);
125
+ } else {
126
+ // fallback: set background color
127
+ el.style.background = mOpt.color;
128
+ }
129
+ } catch (e) {
130
+ // ignore
131
+ }
132
+ }
133
+ }
134
+ }
135
+ } catch (err) {
136
+ // be defensive — don't block marker addition on errors applying defaults
137
+ void err;
138
+ }
88
139
  if (cluster) {
89
140
  this.addPoint(marker, this.markers.length);
90
141
  if (this.mapsService) {
@@ -97,11 +148,36 @@ export default class MarkerLayer extends EventEmitter {
97
148
  }
98
149
  }
99
150
  this.markers.push(marker);
151
+
152
+ // if layer has been added to a scene, immediately add marker's element into scene
153
+ // this ensures addMarker works both before and after addTo(scene)
154
+ try {
155
+ if (this.inited && this.scene && typeof marker.addTo === 'function') {
156
+ // When cluster mode is enabled, defer actual DOM mounting of original markers
157
+ // to the clustering render pass so that only cluster markers (or the chosen
158
+ // original marker for single-point clusters) are attached. This prevents
159
+ // duplicate DOM nodes / missing event handlers caused by pre-mounting originals.
160
+ if (!this.markerLayerOption.cluster) {
161
+ marker.addTo(this.scene);
162
+ }
163
+ }
164
+ } catch (e) {
165
+ // defensive: do not break on addTo errors
166
+ void e;
167
+ }
100
168
  }
101
169
  removeMarker(marker) {
102
170
  this.markers.indexOf(marker);
103
171
  const markerIndex = this.markers.indexOf(marker);
104
172
  if (markerIndex > -1) {
173
+ // remove visual element and unbind handlers
174
+ try {
175
+ if (typeof marker.remove === 'function') {
176
+ marker.remove();
177
+ }
178
+ } catch (e) {
179
+ void e;
180
+ }
105
181
  this.markers.splice(markerIndex, 1);
106
182
  if (this.markerLayerOption.cluster) {
107
183
  this.removePoint(markerIndex);
@@ -116,11 +192,27 @@ export default class MarkerLayer extends EventEmitter {
116
192
  * 隐藏 marker 在每个 marker 上单独修改属性而不是在 markerContainer 上修改(在 markerContainer 修改会有用户在场景加载完之前调用失败的问题)
117
193
  */
118
194
  hide() {
119
- this.markers.map(m => {
120
- m.getElement().style.opacity = '0';
195
+ this.markers.forEach(m => {
196
+ try {
197
+ if (typeof m.hide === 'function') {
198
+ m.hide();
199
+ } else {
200
+ m.getElement().style.opacity = '0';
201
+ }
202
+ } catch (e) {
203
+ void e;
204
+ }
121
205
  });
122
- this.clusterMarkers.map(m => {
123
- m.getElement().style.opacity = '0';
206
+ this.clusterMarkers.forEach(m => {
207
+ try {
208
+ if (typeof m.hide === 'function') {
209
+ m.hide();
210
+ } else {
211
+ m.getElement().style.opacity = '0';
212
+ }
213
+ } catch (e) {
214
+ void e;
215
+ }
124
216
  });
125
217
  }
126
218
 
@@ -128,11 +220,27 @@ export default class MarkerLayer extends EventEmitter {
128
220
  * 显示 marker
129
221
  */
130
222
  show() {
131
- this.markers.map(m => {
132
- m.getElement().style.opacity = '1';
223
+ this.markers.forEach(m => {
224
+ try {
225
+ if (typeof m.show === 'function') {
226
+ m.show();
227
+ } else {
228
+ m.getElement().style.opacity = '1';
229
+ }
230
+ } catch (e) {
231
+ void e;
232
+ }
133
233
  });
134
- this.clusterMarkers.map(m => {
135
- m.getElement().style.opacity = '1';
234
+ this.clusterMarkers.forEach(m => {
235
+ try {
236
+ if (typeof m.show === 'function') {
237
+ m.show();
238
+ } else {
239
+ m.getElement().style.opacity = '1';
240
+ }
241
+ } catch (e) {
242
+ void e;
243
+ }
136
244
  });
137
245
  }
138
246
 
@@ -171,6 +279,25 @@ export default class MarkerLayer extends EventEmitter {
171
279
  this.mapsService.off('viewchange', this.update);
172
280
  this.mapsService.off('camerachange', this.setContainerSize.bind(this));
173
281
  this.mapsService.off('viewchange', this.setContainerSize.bind(this));
282
+ this.mapsService.off('camerachange', this.updateMarkers.bind(this));
283
+ this.mapsService.off('viewchange', this.updateMarkers.bind(this));
284
+ }
285
+ updateMarkers() {
286
+ // update positions for both origin markers and cluster markers
287
+ try {
288
+ this.markers.forEach(m => {
289
+ if (m && typeof m.update === 'function') {
290
+ m.update();
291
+ }
292
+ });
293
+ this.clusterMarkers.forEach(m => {
294
+ if (m && typeof m.update === 'function') {
295
+ m.update();
296
+ }
297
+ });
298
+ } catch (e) {
299
+ void e;
300
+ }
174
301
  }
175
302
 
176
303
  // 将marker数据保存在point中
@@ -253,6 +380,27 @@ export default class MarkerLayer extends EventEmitter {
253
380
  }
254
381
  }
255
382
  const marker = this.clusterMarker(feature);
383
+ // attach layer-level re-emission so consumers can listen to cluster marker events
384
+ try {
385
+ // IMarker type may not declare EventEmitter methods; cast to any for runtime attach
386
+ const anyMarker = marker;
387
+ if (anyMarker && typeof anyMarker.on === 'function') {
388
+ anyMarker.on('click', ev => {
389
+ try {
390
+ this.emit('marker:click', {
391
+ marker: anyMarker,
392
+ data: anyMarker.getExtData ? anyMarker.getExtData() : null,
393
+ lngLat: anyMarker.getLnglat ? anyMarker.getLnglat() : null,
394
+ originalEvent: ev
395
+ });
396
+ } catch (e) {
397
+ void e;
398
+ }
399
+ });
400
+ }
401
+ } catch (e) {
402
+ void e;
403
+ }
256
404
  this.clusterMarkers.push(marker);
257
405
  marker.addTo(this.scene);
258
406
  });
@@ -264,16 +412,65 @@ export default class MarkerLayer extends EventEmitter {
264
412
  return this.clusterIndex.getLeaves(clusterId, limit, offset);
265
413
  }
266
414
  clusterMarker(feature) {
415
+ var _feature$properties3, _feature$properties4;
267
416
  const clusterOption = this.markerLayerOption.clusterOption;
268
417
  const {
269
418
  element = this.generateElement.bind(this)
270
419
  } = clusterOption;
420
+
421
+ // determine cluster count
422
+ let pointCount = (_feature$properties3 = feature.properties) === null || _feature$properties3 === void 0 ? void 0 : _feature$properties3.point_count;
423
+ if (pointCount === undefined && (_feature$properties4 = feature.properties) !== null && _feature$properties4 !== void 0 && _feature$properties4.cluster_id) {
424
+ const leaves = this.getLeaves(feature.properties.cluster_id, Infinity, 0) || [];
425
+ pointCount = leaves.length;
426
+ }
427
+
428
+ // if this cluster effectively contains only one original marker, return the original marker
429
+ if ((pointCount === 1 || pointCount === '1') && feature.properties) {
430
+ // try to get the original marker by marker_id from leaves or properties
431
+ let leaf = null;
432
+ if (feature.properties.cluster_id) {
433
+ const leaves = this.getLeaves(feature.properties.cluster_id, 1, 0);
434
+ leaf = leaves && leaves[0];
435
+ } else if (feature.properties.marker_id !== undefined) {
436
+ leaf = feature;
437
+ }
438
+ if (leaf && leaf.properties && typeof leaf.properties.marker_id === 'number') {
439
+ const origin = this.normalMarker(leaf);
440
+ if (origin) {
441
+ // ensure aggregated properties are available on the original marker
442
+ try {
443
+ if (feature && feature.properties && typeof origin.setExtData === 'function') {
444
+ origin.setExtData(feature.properties);
445
+ }
446
+ } catch (e) {
447
+ void e;
448
+ }
449
+ return origin;
450
+ }
451
+ }
452
+ // fallback: if no marker_id, continue to render cluster element
453
+ }
454
+
455
+ // for real clusters (count > 1) or fallback, create cluster marker element
456
+ let el;
457
+ if (typeof element === 'function') {
458
+ el = element(feature);
459
+ } else {
460
+ // element may be a DOM node already
461
+ el = element;
462
+ }
271
463
  const marker = new Marker({
272
- element: element(feature)
464
+ element: el
273
465
  }).setLnglat({
274
466
  lng: feature.geometry.coordinates[0],
275
467
  lat: feature.geometry.coordinates[1]
276
468
  });
469
+ // attach aggregated properties to the cluster marker so getExtData() returns useful info
470
+ if (feature && feature.properties) {
471
+ // @ts-ignore
472
+ marker.setExtData(feature.properties);
473
+ }
277
474
  return marker;
278
475
  }
279
476
  normalMarker(feature) {
package/es/marker.d.ts CHANGED
@@ -9,6 +9,7 @@ export default class Marker extends EventEmitter {
9
9
  private scene;
10
10
  private added;
11
11
  private preLngLat;
12
+ private visible;
12
13
  getMarkerLayerContainerSize(): IMarkerContainerAndBounds | void;
13
14
  constructor(option?: Partial<IMarkerOption>);
14
15
  getDefault(): {
@@ -21,6 +22,14 @@ export default class Marker extends EventEmitter {
21
22
  };
22
23
  addTo(scene: L7Container): this;
23
24
  remove(): this;
25
+ /**
26
+ * Hide the marker visually but keep it in the layer's data structures.
27
+ */
28
+ hide(): this;
29
+ /**
30
+ * Show the marker if it was hidden.
31
+ */
32
+ show(): this;
24
33
  setLnglat(lngLat: ILngLat | IPoint): this;
25
34
  getLnglat(): ILngLat;
26
35
  getElement(): HTMLElement;
@@ -35,7 +44,7 @@ export default class Marker extends EventEmitter {
35
44
  getDraggable(): boolean;
36
45
  getExtData(): any;
37
46
  setExtData(data: any): void;
38
- private update;
47
+ update(): void;
39
48
  private updatePositionWhenZoom;
40
49
  private onMapClick;
41
50
  private getCurrentContainerSize;
package/es/marker.js CHANGED
@@ -19,6 +19,7 @@ export default class Marker extends EventEmitter {
19
19
  lng: 0,
20
20
  lat: 0
21
21
  });
22
+ _defineProperty(this, "visible", true);
22
23
  _defineProperty(this, "onMarkerDragStart", e => {
23
24
  const mapContainer = this.mapsService.getContainer();
24
25
  if (!mapContainer) {
@@ -105,7 +106,8 @@ export default class Marker extends EventEmitter {
105
106
  } = this.markerOption;
106
107
  this.mapsService.getMarkerContainer().appendChild(element);
107
108
  this.registerMarkerEvent(element);
108
- this.mapsService.on('camerachange', this.update); // 注册高德1.x 的地图事件监听
109
+ // marker 不再在自身注册地图相机事件,以避免为每个 marker 注册大量底层 map 监听器。
110
+ // 由 MarkerLayer 在 addTo 时统一注册并在相机变化时调用每个 marker.update()。
109
111
  this.update();
110
112
  this.updateDraggable();
111
113
  this.added = true;
@@ -114,10 +116,7 @@ export default class Marker extends EventEmitter {
114
116
  }
115
117
  remove() {
116
118
  if (this.mapsService) {
117
- this.mapsService.off('click', this.onMapClick);
118
- this.mapsService.off('move', this.update);
119
- this.mapsService.off('moveend', this.update);
120
- this.mapsService.off('camerachange', this.update);
119
+ // 不再负责移除地图级别的 update 监听(由 MarkerLayer 管理),移除地图事件请统一通过 layer
121
120
  }
122
121
  this.unRegisterMarkerEvent();
123
122
  this.removeAllListeners();
@@ -132,6 +131,36 @@ export default class Marker extends EventEmitter {
132
131
  }
133
132
  return this;
134
133
  }
134
+
135
+ /**
136
+ * Hide the marker visually but keep it in the layer's data structures.
137
+ */
138
+ hide() {
139
+ const {
140
+ element
141
+ } = this.markerOption;
142
+ if (element) {
143
+ element.style.display = 'none';
144
+ }
145
+ this.visible = false;
146
+ return this;
147
+ }
148
+
149
+ /**
150
+ * Show the marker if it was hidden.
151
+ */
152
+ show() {
153
+ const {
154
+ element
155
+ } = this.markerOption;
156
+ if (element) {
157
+ element.style.display = 'block';
158
+ }
159
+ this.visible = true;
160
+ // re-position after showing
161
+ this.update();
162
+ return this;
163
+ }
135
164
  setLnglat(lngLat) {
136
165
  this.lngLat = lngLat;
137
166
  if (Array.isArray(lngLat)) {
@@ -262,6 +291,9 @@ export default class Marker extends EventEmitter {
262
291
  lng,
263
292
  lat
264
293
  } = this.lngLat;
294
+ if (!this.visible) {
295
+ return;
296
+ }
265
297
  if (element) {
266
298
  element.style.display = 'block';
267
299
  element.style.whiteSpace = 'nowrap';
@@ -300,7 +332,6 @@ export default class Marker extends EventEmitter {
300
332
  element.style.transition = 'left 0.25s cubic-bezier(0,0,0.25,1), top 0.25s cubic-bezier(0,0,0.25,1)';
301
333
  }
302
334
  }
303
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
304
335
  onMapClick(e) {
305
336
  const {
306
337
  element
@@ -341,6 +372,13 @@ export default class Marker extends EventEmitter {
341
372
  lat
342
373
  } = this.lngLat;
343
374
  const pos = this.mapsService.lngLatToContainer([lng, lat]);
375
+ if (!this.visible) {
376
+ // remain hidden until show() is called
377
+ if (element) {
378
+ element.style.display = 'none';
379
+ }
380
+ return;
381
+ }
344
382
  if (element) {
345
383
  element.style.display = 'block';
346
384
  element.style.whiteSpace = 'nowrap';
@@ -454,13 +492,9 @@ export default class Marker extends EventEmitter {
454
492
  }
455
493
  }
456
494
  }
457
-
458
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
459
495
  addDragHandler(e) {
460
496
  return null;
461
497
  }
462
-
463
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
464
498
  onUp(e) {
465
499
  throw new Error('Method not implemented.');
466
500
  }
@@ -15,4 +15,15 @@ export interface IMarkerStyleOption {
15
15
  export interface IMarkerLayerOption {
16
16
  cluster: boolean;
17
17
  clusterOption: Partial<IMarkerStyleOption>;
18
+ /**
19
+ * Default marker options applied to markers added to this layer when not overridden per-marker.
20
+ * Example: { color: '#ff0000', style: { width: '24px', height: '24px' }, className: 'my-marker' }
21
+ */
22
+ markerOption?: Partial<{
23
+ color?: string;
24
+ style?: {
25
+ [key: string]: any;
26
+ };
27
+ className?: string;
28
+ }>;
18
29
  }
@@ -43,6 +43,7 @@ export default class MarkerLayer extends EventEmitter {
43
43
  addMarkers(): void;
44
44
  clear(): void;
45
45
  destroy(): void;
46
+ private updateMarkers;
46
47
  private addPoint;
47
48
  private removePoint;
48
49
  private initCluster;
@@ -64,6 +64,9 @@ class MarkerLayer extends _eventemitter.EventEmitter {
64
64
  this.mapsService.on('camerachange', this.update); // amap1.x 更新事件
65
65
  this.mapsService.on('viewchange', this.update); // amap2.0 更新事件
66
66
  }
67
+ // 统一由 layer 管理 marker 的位置更新,避免每个 marker 单独注册地图事件
68
+ this.mapsService.on('camerachange', this.updateMarkers.bind(this));
69
+ this.mapsService.on('viewchange', this.updateMarkers.bind(this));
67
70
  this.mapsService.on('camerachange', this.setContainerSize.bind(this)); // amap1.x 更新事件
68
71
  this.mapsService.on('viewchange', this.setContainerSize.bind(this)); // amap2.0 更新事件
69
72
  this.addMarkers();
@@ -93,6 +96,54 @@ class MarkerLayer extends _eventemitter.EventEmitter {
93
96
  addMarker(marker) {
94
97
  const cluster = this.markerLayerOption.cluster;
95
98
  marker.getMarkerLayerContainerSize = this.getContainerSize.bind(this);
99
+
100
+ // apply default markerOption if provided by layer
101
+ try {
102
+ const mOpt = this.markerLayerOption.markerOption;
103
+ if (mOpt && marker && typeof marker.getElement === 'function') {
104
+ const el = marker.getElement();
105
+ if (el) {
106
+ // apply className
107
+ if (mOpt.className) {
108
+ // DOM helper available
109
+ try {
110
+ _l7Utils.DOM.addClass(el, mOpt.className);
111
+ } catch (e) {
112
+ el.className = `${el.className || ''} ${mOpt.className}`.trim();
113
+ }
114
+ }
115
+ // apply style properties
116
+ if (mOpt.style && typeof mOpt.style === 'object') {
117
+ Object.keys(mOpt.style).forEach(k => {
118
+ try {
119
+ // @ts-ignore
120
+ el.style[k] = mOpt.style[k];
121
+ } catch (e) {
122
+ // ignore invalid styles
123
+ }
124
+ });
125
+ }
126
+ // apply color: try find svg path inside marker element and set fill
127
+ if (mOpt.color) {
128
+ try {
129
+ // querySelector can return SVGPathElement; coerce to any for attribute access
130
+ const svgPath = el.querySelector ? el.querySelector('path') : null;
131
+ if (svgPath && typeof svgPath.setAttribute === 'function') {
132
+ svgPath.setAttribute('fill', mOpt.color);
133
+ } else {
134
+ // fallback: set background color
135
+ el.style.background = mOpt.color;
136
+ }
137
+ } catch (e) {
138
+ // ignore
139
+ }
140
+ }
141
+ }
142
+ }
143
+ } catch (err) {
144
+ // be defensive — don't block marker addition on errors applying defaults
145
+ void err;
146
+ }
96
147
  if (cluster) {
97
148
  this.addPoint(marker, this.markers.length);
98
149
  if (this.mapsService) {
@@ -105,11 +156,36 @@ class MarkerLayer extends _eventemitter.EventEmitter {
105
156
  }
106
157
  }
107
158
  this.markers.push(marker);
159
+
160
+ // if layer has been added to a scene, immediately add marker's element into scene
161
+ // this ensures addMarker works both before and after addTo(scene)
162
+ try {
163
+ if (this.inited && this.scene && typeof marker.addTo === 'function') {
164
+ // When cluster mode is enabled, defer actual DOM mounting of original markers
165
+ // to the clustering render pass so that only cluster markers (or the chosen
166
+ // original marker for single-point clusters) are attached. This prevents
167
+ // duplicate DOM nodes / missing event handlers caused by pre-mounting originals.
168
+ if (!this.markerLayerOption.cluster) {
169
+ marker.addTo(this.scene);
170
+ }
171
+ }
172
+ } catch (e) {
173
+ // defensive: do not break on addTo errors
174
+ void e;
175
+ }
108
176
  }
109
177
  removeMarker(marker) {
110
178
  this.markers.indexOf(marker);
111
179
  const markerIndex = this.markers.indexOf(marker);
112
180
  if (markerIndex > -1) {
181
+ // remove visual element and unbind handlers
182
+ try {
183
+ if (typeof marker.remove === 'function') {
184
+ marker.remove();
185
+ }
186
+ } catch (e) {
187
+ void e;
188
+ }
113
189
  this.markers.splice(markerIndex, 1);
114
190
  if (this.markerLayerOption.cluster) {
115
191
  this.removePoint(markerIndex);
@@ -124,11 +200,27 @@ class MarkerLayer extends _eventemitter.EventEmitter {
124
200
  * 隐藏 marker 在每个 marker 上单独修改属性而不是在 markerContainer 上修改(在 markerContainer 修改会有用户在场景加载完之前调用失败的问题)
125
201
  */
126
202
  hide() {
127
- this.markers.map(m => {
128
- m.getElement().style.opacity = '0';
203
+ this.markers.forEach(m => {
204
+ try {
205
+ if (typeof m.hide === 'function') {
206
+ m.hide();
207
+ } else {
208
+ m.getElement().style.opacity = '0';
209
+ }
210
+ } catch (e) {
211
+ void e;
212
+ }
129
213
  });
130
- this.clusterMarkers.map(m => {
131
- m.getElement().style.opacity = '0';
214
+ this.clusterMarkers.forEach(m => {
215
+ try {
216
+ if (typeof m.hide === 'function') {
217
+ m.hide();
218
+ } else {
219
+ m.getElement().style.opacity = '0';
220
+ }
221
+ } catch (e) {
222
+ void e;
223
+ }
132
224
  });
133
225
  }
134
226
 
@@ -136,11 +228,27 @@ class MarkerLayer extends _eventemitter.EventEmitter {
136
228
  * 显示 marker
137
229
  */
138
230
  show() {
139
- this.markers.map(m => {
140
- m.getElement().style.opacity = '1';
231
+ this.markers.forEach(m => {
232
+ try {
233
+ if (typeof m.show === 'function') {
234
+ m.show();
235
+ } else {
236
+ m.getElement().style.opacity = '1';
237
+ }
238
+ } catch (e) {
239
+ void e;
240
+ }
141
241
  });
142
- this.clusterMarkers.map(m => {
143
- m.getElement().style.opacity = '1';
242
+ this.clusterMarkers.forEach(m => {
243
+ try {
244
+ if (typeof m.show === 'function') {
245
+ m.show();
246
+ } else {
247
+ m.getElement().style.opacity = '1';
248
+ }
249
+ } catch (e) {
250
+ void e;
251
+ }
144
252
  });
145
253
  }
146
254
 
@@ -179,6 +287,25 @@ class MarkerLayer extends _eventemitter.EventEmitter {
179
287
  this.mapsService.off('viewchange', this.update);
180
288
  this.mapsService.off('camerachange', this.setContainerSize.bind(this));
181
289
  this.mapsService.off('viewchange', this.setContainerSize.bind(this));
290
+ this.mapsService.off('camerachange', this.updateMarkers.bind(this));
291
+ this.mapsService.off('viewchange', this.updateMarkers.bind(this));
292
+ }
293
+ updateMarkers() {
294
+ // update positions for both origin markers and cluster markers
295
+ try {
296
+ this.markers.forEach(m => {
297
+ if (m && typeof m.update === 'function') {
298
+ m.update();
299
+ }
300
+ });
301
+ this.clusterMarkers.forEach(m => {
302
+ if (m && typeof m.update === 'function') {
303
+ m.update();
304
+ }
305
+ });
306
+ } catch (e) {
307
+ void e;
308
+ }
182
309
  }
183
310
 
184
311
  // 将marker数据保存在point中
@@ -261,6 +388,27 @@ class MarkerLayer extends _eventemitter.EventEmitter {
261
388
  }
262
389
  }
263
390
  const marker = this.clusterMarker(feature);
391
+ // attach layer-level re-emission so consumers can listen to cluster marker events
392
+ try {
393
+ // IMarker type may not declare EventEmitter methods; cast to any for runtime attach
394
+ const anyMarker = marker;
395
+ if (anyMarker && typeof anyMarker.on === 'function') {
396
+ anyMarker.on('click', ev => {
397
+ try {
398
+ this.emit('marker:click', {
399
+ marker: anyMarker,
400
+ data: anyMarker.getExtData ? anyMarker.getExtData() : null,
401
+ lngLat: anyMarker.getLnglat ? anyMarker.getLnglat() : null,
402
+ originalEvent: ev
403
+ });
404
+ } catch (e) {
405
+ void e;
406
+ }
407
+ });
408
+ }
409
+ } catch (e) {
410
+ void e;
411
+ }
264
412
  this.clusterMarkers.push(marker);
265
413
  marker.addTo(this.scene);
266
414
  });
@@ -272,16 +420,65 @@ class MarkerLayer extends _eventemitter.EventEmitter {
272
420
  return this.clusterIndex.getLeaves(clusterId, limit, offset);
273
421
  }
274
422
  clusterMarker(feature) {
423
+ var _feature$properties3, _feature$properties4;
275
424
  const clusterOption = this.markerLayerOption.clusterOption;
276
425
  const {
277
426
  element = this.generateElement.bind(this)
278
427
  } = clusterOption;
428
+
429
+ // determine cluster count
430
+ let pointCount = (_feature$properties3 = feature.properties) === null || _feature$properties3 === void 0 ? void 0 : _feature$properties3.point_count;
431
+ if (pointCount === undefined && (_feature$properties4 = feature.properties) !== null && _feature$properties4 !== void 0 && _feature$properties4.cluster_id) {
432
+ const leaves = this.getLeaves(feature.properties.cluster_id, Infinity, 0) || [];
433
+ pointCount = leaves.length;
434
+ }
435
+
436
+ // if this cluster effectively contains only one original marker, return the original marker
437
+ if ((pointCount === 1 || pointCount === '1') && feature.properties) {
438
+ // try to get the original marker by marker_id from leaves or properties
439
+ let leaf = null;
440
+ if (feature.properties.cluster_id) {
441
+ const leaves = this.getLeaves(feature.properties.cluster_id, 1, 0);
442
+ leaf = leaves && leaves[0];
443
+ } else if (feature.properties.marker_id !== undefined) {
444
+ leaf = feature;
445
+ }
446
+ if (leaf && leaf.properties && typeof leaf.properties.marker_id === 'number') {
447
+ const origin = this.normalMarker(leaf);
448
+ if (origin) {
449
+ // ensure aggregated properties are available on the original marker
450
+ try {
451
+ if (feature && feature.properties && typeof origin.setExtData === 'function') {
452
+ origin.setExtData(feature.properties);
453
+ }
454
+ } catch (e) {
455
+ void e;
456
+ }
457
+ return origin;
458
+ }
459
+ }
460
+ // fallback: if no marker_id, continue to render cluster element
461
+ }
462
+
463
+ // for real clusters (count > 1) or fallback, create cluster marker element
464
+ let el;
465
+ if (typeof element === 'function') {
466
+ el = element(feature);
467
+ } else {
468
+ // element may be a DOM node already
469
+ el = element;
470
+ }
279
471
  const marker = new _marker.default({
280
- element: element(feature)
472
+ element: el
281
473
  }).setLnglat({
282
474
  lng: feature.geometry.coordinates[0],
283
475
  lat: feature.geometry.coordinates[1]
284
476
  });
477
+ // attach aggregated properties to the cluster marker so getExtData() returns useful info
478
+ if (feature && feature.properties) {
479
+ // @ts-ignore
480
+ marker.setExtData(feature.properties);
481
+ }
285
482
  return marker;
286
483
  }
287
484
  normalMarker(feature) {
package/lib/marker.d.ts CHANGED
@@ -9,6 +9,7 @@ export default class Marker extends EventEmitter {
9
9
  private scene;
10
10
  private added;
11
11
  private preLngLat;
12
+ private visible;
12
13
  getMarkerLayerContainerSize(): IMarkerContainerAndBounds | void;
13
14
  constructor(option?: Partial<IMarkerOption>);
14
15
  getDefault(): {
@@ -21,6 +22,14 @@ export default class Marker extends EventEmitter {
21
22
  };
22
23
  addTo(scene: L7Container): this;
23
24
  remove(): this;
25
+ /**
26
+ * Hide the marker visually but keep it in the layer's data structures.
27
+ */
28
+ hide(): this;
29
+ /**
30
+ * Show the marker if it was hidden.
31
+ */
32
+ show(): this;
24
33
  setLnglat(lngLat: ILngLat | IPoint): this;
25
34
  getLnglat(): ILngLat;
26
35
  getElement(): HTMLElement;
@@ -35,7 +44,7 @@ export default class Marker extends EventEmitter {
35
44
  getDraggable(): boolean;
36
45
  getExtData(): any;
37
46
  setExtData(data: any): void;
38
- private update;
47
+ update(): void;
39
48
  private updatePositionWhenZoom;
40
49
  private onMapClick;
41
50
  private getCurrentContainerSize;
package/lib/marker.js CHANGED
@@ -25,6 +25,7 @@ class Marker extends _eventemitter.EventEmitter {
25
25
  lng: 0,
26
26
  lat: 0
27
27
  });
28
+ (0, _defineProperty2.default)(this, "visible", true);
28
29
  (0, _defineProperty2.default)(this, "onMarkerDragStart", e => {
29
30
  const mapContainer = this.mapsService.getContainer();
30
31
  if (!mapContainer) {
@@ -111,7 +112,8 @@ class Marker extends _eventemitter.EventEmitter {
111
112
  } = this.markerOption;
112
113
  this.mapsService.getMarkerContainer().appendChild(element);
113
114
  this.registerMarkerEvent(element);
114
- this.mapsService.on('camerachange', this.update); // 注册高德1.x 的地图事件监听
115
+ // marker 不再在自身注册地图相机事件,以避免为每个 marker 注册大量底层 map 监听器。
116
+ // 由 MarkerLayer 在 addTo 时统一注册并在相机变化时调用每个 marker.update()。
115
117
  this.update();
116
118
  this.updateDraggable();
117
119
  this.added = true;
@@ -120,10 +122,7 @@ class Marker extends _eventemitter.EventEmitter {
120
122
  }
121
123
  remove() {
122
124
  if (this.mapsService) {
123
- this.mapsService.off('click', this.onMapClick);
124
- this.mapsService.off('move', this.update);
125
- this.mapsService.off('moveend', this.update);
126
- this.mapsService.off('camerachange', this.update);
125
+ // 不再负责移除地图级别的 update 监听(由 MarkerLayer 管理),移除地图事件请统一通过 layer
127
126
  }
128
127
  this.unRegisterMarkerEvent();
129
128
  this.removeAllListeners();
@@ -138,6 +137,36 @@ class Marker extends _eventemitter.EventEmitter {
138
137
  }
139
138
  return this;
140
139
  }
140
+
141
+ /**
142
+ * Hide the marker visually but keep it in the layer's data structures.
143
+ */
144
+ hide() {
145
+ const {
146
+ element
147
+ } = this.markerOption;
148
+ if (element) {
149
+ element.style.display = 'none';
150
+ }
151
+ this.visible = false;
152
+ return this;
153
+ }
154
+
155
+ /**
156
+ * Show the marker if it was hidden.
157
+ */
158
+ show() {
159
+ const {
160
+ element
161
+ } = this.markerOption;
162
+ if (element) {
163
+ element.style.display = 'block';
164
+ }
165
+ this.visible = true;
166
+ // re-position after showing
167
+ this.update();
168
+ return this;
169
+ }
141
170
  setLnglat(lngLat) {
142
171
  this.lngLat = lngLat;
143
172
  if (Array.isArray(lngLat)) {
@@ -268,6 +297,9 @@ class Marker extends _eventemitter.EventEmitter {
268
297
  lng,
269
298
  lat
270
299
  } = this.lngLat;
300
+ if (!this.visible) {
301
+ return;
302
+ }
271
303
  if (element) {
272
304
  element.style.display = 'block';
273
305
  element.style.whiteSpace = 'nowrap';
@@ -306,7 +338,6 @@ class Marker extends _eventemitter.EventEmitter {
306
338
  element.style.transition = 'left 0.25s cubic-bezier(0,0,0.25,1), top 0.25s cubic-bezier(0,0,0.25,1)';
307
339
  }
308
340
  }
309
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
310
341
  onMapClick(e) {
311
342
  const {
312
343
  element
@@ -347,6 +378,13 @@ class Marker extends _eventemitter.EventEmitter {
347
378
  lat
348
379
  } = this.lngLat;
349
380
  const pos = this.mapsService.lngLatToContainer([lng, lat]);
381
+ if (!this.visible) {
382
+ // remain hidden until show() is called
383
+ if (element) {
384
+ element.style.display = 'none';
385
+ }
386
+ return;
387
+ }
350
388
  if (element) {
351
389
  element.style.display = 'block';
352
390
  element.style.whiteSpace = 'nowrap';
@@ -460,13 +498,9 @@ class Marker extends _eventemitter.EventEmitter {
460
498
  }
461
499
  }
462
500
  }
463
-
464
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
465
501
  addDragHandler(e) {
466
502
  return null;
467
503
  }
468
-
469
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
470
504
  onUp(e) {
471
505
  throw new Error('Method not implemented.');
472
506
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antv/l7-component",
3
- "version": "2.23.1",
3
+ "version": "2.23.2",
4
4
  "description": "Component for L7",
5
5
  "license": "MIT",
6
6
  "author": "https://github.com/orgs/antvis/people",
@@ -16,13 +16,13 @@
16
16
  "@babel/runtime": "^7.7.7",
17
17
  "eventemitter3": "^4.0.0",
18
18
  "supercluster": "^7.0.0",
19
- "@antv/l7-core": "2.23.1",
20
- "@antv/l7-layers": "2.23.1",
21
- "@antv/l7-utils": "2.23.1"
19
+ "@antv/l7-core": "2.23.2",
20
+ "@antv/l7-layers": "2.23.2",
21
+ "@antv/l7-utils": "2.23.2"
22
22
  },
23
23
  "devDependencies": {
24
24
  "less": "^4.1.3",
25
- "@antv/l7-test-utils": "^2.23.1"
25
+ "@antv/l7-test-utils": "^2.23.2"
26
26
  },
27
27
  "publishConfig": {
28
28
  "access": "public",