You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

90 lines
2.2 KiB

/**
* 地图助手(Mapbox)
* replace/restore 移动 #map 容器后必须 resize()
*/
class MapHelper {
_map = null
_originParent = null
_savedView = null
setMap(map) {
this._map = map
}
getMap() {
return this._map
}
toggleMapMode({ is2D = false, is3D = false } = {}) {
if (!this._map) return
if (is3D) {
this._map.easeTo({ pitch: 45, bearing: 0, duration: 0 })
this._map.dragRotate.enable()
this._map.touchZoomRotate.enableRotation()
this._map.dragRotate.enablePitch?.()
this._map.touchPitch?.enable?.()
}
if (is2D) {
// 只锁俯仰/旋转;投影由 setProjection 单独控制
// 监控要 globe 圆形地球,航线编辑要 mercator
this._map.easeTo({ pitch: 0, bearing: 0, duration: 0 })
this._map.dragRotate.disable()
this._map.touchZoomRotate.disableRotation()
this._map.dragRotate.disablePitch?.()
this._map.touchPitch?.disable?.()
}
}
setProjection(name) {
if (!this._map || !name) return
try {
this._map.setProjection(name)
} catch (_) {
/* ignore */
}
}
replaceMapContainer(newParent) {
if (!this._map || !newParent) return
const el = this._map.getContainer()
this._originParent = this._originParent || el.parentNode
const c = this._map.getCenter()
this._savedView = {
center: [c.lng, c.lat],
zoom: this._map.getZoom(),
pitch: this._map.getPitch(),
bearing: this._map.getBearing(),
}
newParent.appendChild(el)
this._map.resize()
}
restoreMapContainer() {
if (!this._map || !this._originParent) return
const el = this._map.getContainer()
this._originParent.appendChild(el)
if (this._savedView) {
this._map.jumpTo({
center: this._savedView.center,
zoom: this._savedView.zoom,
pitch: this._savedView.pitch,
bearing: this._savedView.bearing,
})
}
this._map.resize()
this._originParent = null
this._savedView = null
}
}
const g = typeof globalThis !== 'undefined' ? globalThis : undefined
const singleton = (g && g.__LAIC_MAP_HELPER__) || new MapHelper()
if (g) g.__LAIC_MAP_HELPER__ = singleton
export default singleton