Browse Source

优化marker图层切换

master
xh 4 days ago
parent
commit
e7484c884c
  1. 2
      web/src/views/HandDevice/Home/components/constants/map.constants.ts
  2. 58
      web/src/views/HandDevice/Home/components/services/marker.service.ts

2
web/src/views/HandDevice/Home/components/constants/map.constants.ts

@ -50,7 +50,7 @@ export const MAP_DEFAULTS = {
center: [116.3912757, 39.906217] as [number, number], center: [116.3912757, 39.906217] as [number, number],
zoom: 10, zoom: 10,
maxZoom: 18, maxZoom: 18,
minZoom: 0,
minZoom: 3,
enableCluster: true, enableCluster: true,
clusterDistance: 1 clusterDistance: 1
} }

58
web/src/views/HandDevice/Home/components/services/marker.service.ts

@ -15,6 +15,8 @@ import { ANIMATION_CONFIG } from '../constants/map.constants'
export class MarkerService { export class MarkerService {
private map: Map | null = null private map: Map | null = null
markerLayer: VectorLayer<VectorSource | Cluster> | null = null markerLayer: VectorLayer<VectorSource | Cluster> | null = null
// 当前图层模式(single或cluster聚合)
private currentLayerMode: 'single' | 'cluster' | '' = ''
private vectorSource: VectorSource private vectorSource: VectorSource
constructor(map: Map) { constructor(map: Map) {
@ -32,12 +34,11 @@ export class MarkerService {
*/ */
createMarkerLayer(props: MapProps) { createMarkerLayer(props: MapProps) {
// console.log('createMarkerLayer') // console.log('createMarkerLayer')
if (this.markerLayer) {
// this.map?.removeLayer(this.markerLayer)
this.updateData(props)
} else {
this.createMarkerLayerFromProps(props)
}
// this.map?.removeLayer(this.markerLayer)
this.updateData(props)
this.createMarkerLayerFromProps(props)
} }
/** /**
* *
@ -54,26 +55,14 @@ export class MarkerService {
feature.setStyle(createMarkerStyle(marker)) feature.setStyle(createMarkerStyle(marker))
this.vectorSource.addFeature(feature) this.vectorSource.addFeature(feature)
}) })
// this.createMarkerLayerFromProps(props)
}
updateLayer(props: MapProps): void {
if (this.markerLayer) {
this.map?.removeLayer(this.markerLayer)
}
this.createMarkerLayerFromProps(props)
}
/**
*
*/
getMarkerLayer(): VectorLayer<VectorSource | Cluster> | null {
return this.markerLayer
} }
/** /**
* props创建markerLayer * props创建markerLayer
*
*/ */
private createMarkerLayerFromProps(props: MapProps): VectorLayer<VectorSource | Cluster> {
// : VectorLayer<VectorSource | Cluster>
private createMarkerLayerFromProps(props: MapProps) {
this.updateData(props) this.updateData(props)
// 检查是否应该强制使用单个marker模式 // 检查是否应该强制使用单个marker模式
@ -84,17 +73,19 @@ export class MarkerService {
// return currentZoom && currentZoom >= props.forceSingleMark // return currentZoom && currentZoom >= props.forceSingleMark
return currentZoom && currentZoom >= ANIMATION_CONFIG.clusterThreshold return currentZoom && currentZoom >= ANIMATION_CONFIG.clusterThreshold
} }
// console.log('createMarkerLayerFromProps shouldForceSingleMark', shouldForceSingleMark())
// 如果启用聚合且不强制使用单个marker模式
// console.log('shouldForceSingleMark()', shouldForceSingleMark())
let newLayer: VectorLayer<VectorSource | Cluster> | null = null
// 如果启用聚合且不强制使用单个marker模式
if (props.enableCluster && !shouldForceSingleMark()) { if (props.enableCluster && !shouldForceSingleMark()) {
if (this.currentLayerMode === 'cluster') return
this.currentLayerMode = 'cluster'
const clusterSource = new Cluster({ const clusterSource = new Cluster({
source: this.vectorSource, source: this.vectorSource,
distance: Math.max(props.clusterDistance || 10, 10) distance: Math.max(props.clusterDistance || 10, 10)
}) })
this.markerLayer = new VectorLayer({
newLayer = new VectorLayer({
source: clusterSource, source: clusterSource,
zIndex: 1, zIndex: 1,
style: (feature) => { style: (feature) => {
@ -117,9 +108,12 @@ export class MarkerService {
} }
}) })
} else { } else {
if (this.currentLayerMode === 'single') return
this.currentLayerMode = 'single'
// console.log('基础marker') // console.log('基础marker')
this.markerLayer = new VectorLayer({
newLayer = new VectorLayer({
source: this.vectorSource, source: this.vectorSource,
zIndex: 1, zIndex: 1,
renderOrder: (a, b) => { renderOrder: (a, b) => {
@ -130,9 +124,19 @@ export class MarkerService {
} }
}) })
} }
if (this.markerLayer) {
const isVisible = this.markerLayer?.getVisible() || false
newLayer.setVisible(isVisible)
this.map?.removeLayer(this.markerLayer)
}
this.markerLayer = newLayer
this.map?.addLayer(this.markerLayer) this.map?.addLayer(this.markerLayer)
return this.markerLayer
// 获取所有图层
const layers = this.map?.getLayers().getArray() || []
console.log('this.currentLayerMode', this.currentLayerMode, layers)
} }
/** /**

Loading…
Cancel
Save