import EventBus from '@/utils/eventBus'
|
import * as Cesium from 'cesium'
|
import { Cartesian3, Terrain, Viewer } from 'cesium'
|
import store from '@/store/index'
|
|
import getBaseConfig from '@/buildConfig/config'
|
|
const {
|
imageryProvider_ammap,
|
imageryProvider_ammapBz,
|
imageryProvider_ammapSL,
|
imageryProvider_stand,
|
imageryProvider_standZh,
|
imageryProvider_tdt_cia_w,
|
imageryProvider_tdt_img_w,
|
|
blueImageryProviderVec,
|
blueImageryProviderCva,
|
} = getBaseConfig()
|
|
import { addBlueFilter } from '@/utils/cesium/common'
|
import { useBoundary } from '@/utils/cesium/useBoundary'
|
|
let smallMpaCenterDistance = null
|
let curCameraHpr = null
|
|
let proHandler = null
|
let proController = null
|
let proMoveFlag = null
|
|
let handler = null
|
/**
|
* 公共的Cesium (蓝色,2d)
|
*/
|
|
// 封装的不用管
|
let cn = {
|
ri: function () {
|
return a
|
},
|
|
LS: function () {
|
return o
|
},
|
|
jL: function () {
|
return n
|
},
|
}
|
function a (e) {
|
return null != e
|
}
|
function o (e, t) {
|
return a(e) ? e : t
|
}
|
|
function n (e, t = !1) {
|
let { longitude: i, latitude: a, height: o } = e
|
return Cesium.Cartesian3.fromDegrees(i, a, t ? 0 : o)
|
}
|
|
/**
|
* 创建ztzf-cesium
|
* @param {Object} options - 配置选项对象
|
* @param {string} options.dom - Cesium 容器的 DOM 元素
|
* @param {boolean} [options.flatMode=true] - 是否启用 2D 模式,默认为 true
|
* @param {boolean} [options.terrain=false] - 是否启用地形,默认为 false
|
* @param {number} [options.layerMode=17] - 图层模式,默认值为 17(蓝色底图)
|
* @param {boolean} [options.contour=true] - 是否显示轮廓线,默认为 true
|
* @param {boolean} [options.flyToContour=false] - 是否飞行到轮廓,默认为 false
|
* @param {number} [options.multiple=1.4] - 范围倍数
|
* @param {number} [options.dockOptions={}] - 机巢显示
|
* @param {array} [options.boundaryChange] - 边界改变事件
|
* @param {array} [options.boundaryColor] - 边界颜色
|
* @param {array} [options.dockRangeType] - 机巢范围类型 1显示覆盖圆;2显示覆盖圈
|
* @param {array} [options.useDockHeight] - 是否使用机巢高度,默认为false
|
*/
|
export class PublicCesium {
|
|
constructor(options) {
|
this.handlerLeftEvents = { handler: null }
|
this.handlerRightEvents = { handler: null }
|
this.handlerMouseMoveEvents = { handler: null }
|
this.viewer = null
|
this.globalBaseMapLayers = []
|
this.boundary = null
|
this.init(options)
|
}
|
|
init (options) {
|
const {
|
dom,
|
flatMode = true,
|
terrain = false,
|
layerMode = 0,
|
contour = true,
|
// flyToContour = false,
|
multiple = 1.4,
|
|
dockOptions = {},
|
|
|
|
dockRangeType = 1,
|
useDockHeight = false
|
} = options
|
Cesium.Camera.DEFAULT_VIEW_FACTOR = -0.45
|
// 西南东北,默认显示中国
|
Cesium.Camera.DEFAULT_VIEW_RECTANGLE = Cesium.Rectangle.fromDegrees(66, 4, 135, 53.55)
|
const viewerOptions = {
|
infoBox: false, // 禁用沙箱,解决控制台报错
|
animation: false, // 左下角的动画仪表盘
|
baseLayerPicker: false, // 右上角的图层选择按钮
|
geocoder: false, // 搜索框
|
homeButton: false, // home按钮
|
sceneModePicker: false, // 模式切换按钮
|
timeline: false, // 底部的时间轴
|
navigationHelpButton: false, // 右上角的帮助按钮,
|
selectionIndicator: false, // 是否显示选择指示器
|
baseLayer: false,
|
fullscreenButton: false,
|
}
|
|
this.viewer = new Viewer(dom, viewerOptions)
|
this.viewerDom = dom
|
|
this.viewer.scene.globe.enableLighting = true
|
this.viewer.shadows = false
|
this.viewer.shadowMap.darkness = 0.5
|
|
let time = new Date()
|
time.setHours(12, 0, 0, 0)
|
let utc = Cesium.JulianDate.fromDate(new Date(time))
|
|
this.viewer.clockViewModel.currentTime = Cesium.JulianDate.addHours(utc, 0, new Cesium.JulianDate())
|
|
// 边界,机巢
|
|
this.viewer?.imageryLayers.removeAll()
|
this.viewer._cesiumWidget._creditContainer.style.display = 'none'
|
handler = new Cesium.ScreenSpaceEventHandler(this.viewer?.scene.canvas)
|
|
this.viewer?.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK) // 禁用双击
|
|
if (terrain) {
|
try {
|
Cesium.CesiumTerrainProvider.fromUrl(`${import.meta.env.VITE_APP_TERRAIN_URL}`, {
|
requestVertexNormals: true, // 启用地形法线增强立体感
|
requestWaterMask: true, // 启用水体遮罩效果
|
}).then(terrainProvider => {
|
this.viewer.terrainProvider = terrainProvider
|
})
|
} catch (error) {
|
console.error('地形加载失败:', error)
|
}
|
}
|
|
this.viewer.scene.screenSpaceCameraController.maximumZoomDistance = 4500000
|
this.switchLayers(layerMode)
|
this.switchFlatMode(flatMode)
|
// this.switchContour(contour)
|
// flyToContour && this.flyToContour(contour)
|
}
|
|
getViewer () {
|
return this.viewer
|
}
|
|
// 销毁viewer
|
viewerDestroy (removeDom = true) {
|
smallMpaCenterDistance = null
|
|
if (this.viewer) {
|
this.viewer?.entities.removeAll()
|
this.viewer?.imageryLayers.removeAll()
|
this.viewer?.dataSources.removeAll()
|
|
|
// try {
|
// this.viewer?.scene?.primitives?.removeAll()
|
// this.viewer?.entities?.removeAll()
|
// } catch (e) {
|
// console.warn('清理资源时出错:', e)
|
|
// console.log(this.viewer, 1111111111111)
|
// }
|
|
// 获取webgl上下文
|
let gl = this.viewer?.scene.context._originalGLContext
|
gl.canvas.width = 1
|
gl.canvas.height = 1
|
gl.getExtension("WEBGL_lose_context").loseContext()
|
gl = null
|
this.viewer?.destroy() // 销毁Viewer实例
|
this.viewer = null
|
var cesiumContainer = document.getElementById(this.viewerDom)
|
if (cesiumContainer && removeDom) {
|
cesiumContainer.remove() // 移除与地图相关的DOM元素
|
this.viewerDom = null
|
}
|
}
|
}
|
|
setShowDock (sns) {
|
// this.boundary?.setShowDock(sns)
|
}
|
|
// 切换轮廓显示
|
// async switchContour (open) {
|
// if (open) {
|
// await this.boundary?.openContour()
|
// } else {
|
// this.boundary?.closeContour()
|
// }
|
// }
|
// // 飞向轮廓居中
|
// flyToContour () {
|
// this.boundary?.flyToBoundary()
|
// }
|
|
// 飞行 flyto
|
flyTo (pointOption, time = 4, height = 3000, orientation = {}, complete = () => { }) {
|
if (!pointOption.longitude && !pointOption.latitude) return
|
const destination = Cesium.Cartesian3.fromDegrees(pointOption.longitude, pointOption.latitude, height)
|
const duration = time
|
this.viewer?.camera.flyTo({
|
destination,
|
duration,
|
orientation,
|
complete,
|
})
|
}
|
|
switchLayers (mode, roadLine = false) {
|
let mapLayers = []
|
this.globalBaseMapLayers?.forEach(item => {
|
if (item.mapLayer) item.mapLayer.show = false
|
})
|
// store.state.common.mapSetting.mode = mode
|
// 标准地图(天地图矢量)加载
|
if (mode === 0) {
|
mapLayers.push(
|
...[
|
{ key: 'imageryProvider_standZh', layer: imageryProvider_standZh },
|
{ key: 'imageryProvider_stand', layer: imageryProvider_stand },
|
]
|
)
|
}
|
// 高德地图矢量图层加载
|
if (mode === 2) {
|
mapLayers.push({
|
key: 'imageryProvider_ammapSL',
|
layer: imageryProvider_ammapSL,
|
})
|
}
|
// 高德影像地图数据图层
|
if (mode === 3) {
|
mapLayers.push(
|
...[
|
{ key: 'imageryProvider_ammap', layer: imageryProvider_ammap },
|
{ key: 'imageryProvider_ammapBz', layer: imageryProvider_ammapBz },
|
]
|
)
|
}
|
|
// 天地图影像➕注记
|
if (mode === 4) {
|
mapLayers.push(
|
...[
|
{ key: 'imageryProvider_standZh', layer: imageryProvider_standZh },
|
// { key: 'imageryProvider_tdt_cia_w', layer: imageryProvider_tdt_cia_w },
|
{ key: 'imageryProvider_tdt_img_w', layer: imageryProvider_tdt_img_w },
|
]
|
)
|
}
|
|
// 蓝色底图
|
if (mode === 17) {
|
mapLayers.push(
|
...[
|
{ key: 'blueImageryProviderCva', layer: blueImageryProviderCva },
|
{ key: 'blueImageryProviderVec', layer: blueImageryProviderVec },
|
]
|
)
|
}
|
|
// 路线图加载
|
if (roadLine === true && mode === 1) {
|
mapLayers.push({ key: 'annotation', layer: imageryProvider_tdt_cia_w })
|
}
|
|
mapLayers.forEach(item => {
|
let find = this.globalBaseMapLayers.find(item1 => item1.key === item.key)
|
if (!find) {
|
find = {
|
key: item.key,
|
mapLayer: this.viewer?.imageryLayers.addImageryProvider(item.layer),
|
}
|
this.globalBaseMapLayers.push(find)
|
}
|
find.mapLayer.show = true
|
this.setCurrentLayerFilter(find, mode)
|
this.viewer?.imageryLayers.lowerToBottom(find.mapLayer)
|
})
|
}
|
|
// 设置或重置滤镜
|
|
setCurrentLayerFilter (data, mode) {
|
if (mode === 17) {
|
data.mapLayer.brightness = 0.6 // 亮度(默认值)
|
data.mapLayer.contrast = 1.8 // 对比度(负值降低对比度)
|
data.mapLayer.hue = 1 // 色相(弧度值,3.0可能导致紫色)
|
data.mapLayer.saturation = 0 // 增加饱和度强化蓝色
|
data.mapLayer.gamma = 0.3 // 伽马校正(小于1.0使颜色更鲜艳)
|
|
if (data.key === 'blueImageryProviderVec')
|
addBlueFilter(
|
{
|
bInvertColor: true,
|
bFilterColor: true,
|
filterColor: '#4e70a6',
|
},
|
this.viewer,
|
true
|
)
|
} else {
|
data.mapLayer.brightness = 1 // 亮度(默认值)
|
data.mapLayer.contrast = 1 // 对比度(负值降低对比度)
|
data.mapLayer.hue = 0 // 色相(弧度值,3.0可能导致紫色)
|
data.mapLayer.saturation = 1 // 增加饱和度强化蓝色
|
data.mapLayer.gamma = 1 // 伽马校正(小于1.0使颜色更鲜艳)
|
|
addBlueFilter(
|
{
|
bInvertColor: true,
|
bFilterColor: true,
|
filterColor: '#4e70a6',
|
},
|
this.viewer,
|
false
|
)
|
}
|
}
|
|
// 切换为二三维模式 type: true 2D false 3D
|
switchFlatMode (type) {
|
if (type) {
|
this.viewer?.scene.morphTo2D(0)
|
} else {
|
this.viewer?.scene.morphTo3D(0)
|
}
|
}
|
|
// 右键点击事件
|
addRightClickEvent (sid, cb, eventKey = 'rightHandler') {
|
if (this.handlerRightEvents.rightHandler) this.removeRightClickEvent()
|
if (this.handlerRightEvents[eventKey]) this.removeRightClickEvent(eventKey)
|
|
this.handlerRightEvents[eventKey] = new Cesium.ScreenSpaceEventHandler(this.viewer?.scene.canvas)
|
|
this.handlerRightEvents[eventKey].setInputAction(click => {
|
const pick = this.viewer?.scene.pick(click.position)
|
cb(click, pick, this.viewer)
|
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
|
}
|
|
// 移除鼠标右键事件
|
removeRightClickEvent (eventKey = 'rightHandler') {
|
if (!this.handlerRightEvents[eventKey]) return
|
this.handlerRightEvents[eventKey].removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK)
|
|
this.handlerRightEvents[eventKey] = null
|
}
|
|
addLeftDrillPickEvent (cb, eventKey = 'handler') {
|
if (this.handlerLeftEvents.handler) this.removeLeftDrillPickEvent()
|
if (this.handlerLeftEvents[eventKey]) this.removeLeftDrillPickEvent(eventKey)
|
|
this.handlerLeftEvents[eventKey] = new Cesium.ScreenSpaceEventHandler(this.viewer?.scene.canvas)
|
this.handlerLeftEvents[eventKey].setInputAction(click => {
|
const picks = this.viewer?.scene.drillPick(click.position)
|
|
cb(picks)
|
}, Cesium.ScreenSpaceEventType.LEFT_CLICK)
|
}
|
|
removeLeftDrillPickEvent (eventKey = 'handler') {
|
if (this.handlerLeftEvents[eventKey] == null) return
|
this.handlerLeftEvents[eventKey].removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
|
|
this.handlerLeftEvents[eventKey] = null
|
}
|
|
// 鼠标点击事件
|
addLeftClickEvent (sid, cb, eventKey = 'handler') {
|
if (this.handlerLeftEvents.handler) this.removeLeftClickEvent()
|
if (this.handlerLeftEvents[eventKey]) this.removeLeftClickEvent(eventKey)
|
|
this.handlerLeftEvents[eventKey] = new Cesium.ScreenSpaceEventHandler(this.viewer?.scene.canvas)
|
this.handlerLeftEvents[eventKey].setInputAction(click => {
|
const pick = this.viewer?.scene.pick(click.position)
|
|
if (pick && pick.primitive && pick.primitive?.customParams?.type === '3DTileset') {
|
return cb(click, pick, this.viewer, this.handlerLeftEvents[eventKey])
|
}
|
|
if (pick && pick.id && (pick.id._id === sid || pick.id._id.includes(sid))) {
|
cb(click, pick, this.viewer, this.handlerLeftEvents[eventKey])
|
}
|
|
if (sid === null) {
|
cb(click, pick, this.viewer, this.handlerLeftEvents[eventKey])
|
}
|
}, Cesium.ScreenSpaceEventType.LEFT_CLICK)
|
}
|
|
// 移除鼠标点击事件
|
removeLeftClickEvent (eventKey = 'handler') {
|
if (this.handlerLeftEvents[eventKey] == null) return
|
this.handlerLeftEvents[eventKey].removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
|
|
this.handlerLeftEvents[eventKey] = null
|
}
|
|
// 鼠标移动事件
|
addMouseHandler (sid, cb, eventKey = 'mouseHandler') {
|
const that = this
|
if (this.handlerMouseMoveEvents.mouseHandler) this.removeMouseHandler()
|
if (this.handlerMouseMoveEvents[eventKey]) this.removeMouseHandler(eventKey)
|
|
|
this.handlerMouseMoveEvents[eventKey] = new Cesium.ScreenSpaceEventHandler(this.viewer?.scene.canvas)
|
|
this.handlerMouseMoveEvents[eventKey].setInputAction(function (movement) {
|
cb(movement, that.viewer)
|
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
|
}
|
|
// 移除鼠标移动事件
|
removeMouseHandler (eventKey = 'mouseHandler') {
|
if (this.handlerMouseMoveEvents[eventKey] == null) return
|
this.handlerMouseMoveEvents[eventKey].removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
|
|
this.handlerMouseMoveEvents[eventKey] = null
|
}
|
|
pointInitEvent (left_down, mouse_move, left_up) {
|
if (this.pointEditHandler) this.pointUnInitEvent()
|
this.pointEditHandler = new Cesium.ScreenSpaceEventHandler(this.viewer?.scene.canvas)
|
const events = {
|
left_down,
|
mouse_move,
|
left_up,
|
}
|
|
this.pointEditBindEvent(events)
|
this.pointEditBindEvent(events, Cesium.KeyboardEventModifier.CTRL)
|
this.pointEditBindEvent(events, Cesium.KeyboardEventModifier.ALT)
|
this.pointEditBindEvent(events, Cesium.KeyboardEventModifier.SHIFT)
|
this.pointEditBindEvent(events, Cesium.KeyboardEventModifier.META)
|
}
|
|
pointEditBindEvent (events, e) {
|
if (this.pointEditHandler) {
|
this.pointEditHandler.setInputAction(events.left_down(e), Cesium.ScreenSpaceEventType.LEFT_DOWN, e)
|
this.pointEditHandler.setInputAction(events.mouse_move(e), Cesium.ScreenSpaceEventType.MOUSE_MOVE, e)
|
this.pointEditHandler.setInputAction(events.left_up(e), Cesium.ScreenSpaceEventType.LEFT_UP, e)
|
}
|
}
|
|
pointUnInitEvent () {
|
this.pointEditUnBindEvent()
|
this.pointEditUnBindEvent(Cesium.KeyboardEventModifier.CTRL)
|
this.pointEditUnBindEvent(Cesium.KeyboardEventModifier.ALT)
|
this.pointEditUnBindEvent(Cesium.KeyboardEventModifier.SHIFT)
|
this.pointEditUnBindEvent(Cesium.KeyboardEventModifier.META)
|
|
this.pointEditHandler = null
|
}
|
|
pointEditUnBindEvent (e) {
|
this.pointEditHandler?.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOWN, e)
|
this.pointEditHandler?.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE, e)
|
this.pointEditHandler?.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_UP, e)
|
}
|
|
setCameraMove (e) {
|
this.setCameraAttribute(e)
|
}
|
|
setCameraAttribute (e) {
|
let t = {}
|
, i = !1
|
|
if (e.attitude) {
|
let { fov: n } = e.attitude
|
|
if ((0, cn.ri)(n) && this.viewer?.camera) {
|
this.viewer.camera.frustum = new Cesium.PerspectiveFrustum({
|
fov: Cesium.Math.toRadians(n),
|
aspectRatio: this.viewer?.canvas.clientWidth / this.viewer?.canvas.clientHeight,
|
near: .1
|
})
|
}
|
}
|
i && this.viewer?.camera.setView(t)
|
}
|
|
setView (options) {
|
const position = Cesium.Cartesian3.fromDegrees(
|
Number(options.longitude),
|
Number(options.latitude),
|
Number(options.height || 0),
|
)
|
const heading = Number(options.heading || 0)
|
this.viewer?.camera?.setView({
|
destination: position,
|
orientation: {
|
heading: Cesium.Math.toRadians(heading >= 0 ? heading : heading + 360), // 水平旋转(偏航角)
|
pitch: Cesium.Math.toRadians(Number(options.pitch || 0)), // 俯仰角(-90°表示俯视地心)
|
roll: 0.0 // 翻转角
|
}
|
})
|
|
this.getCameraCurPositionDistance()
|
}
|
|
getCameraCurPositionDistance () {
|
if (!this.viewer) return
|
|
// 获取相机位置
|
const position = this.viewer?.camera.positionCartographic
|
const cameraLongitude = Cesium.Math.toDegrees(position.longitude)
|
const cameraLatitude = Cesium.Math.toDegrees(position.latitude)
|
const cameraHeight = position.height
|
|
// 获取地图区域中心点位置
|
let mapCenterPx = {
|
x: this.viewer?.canvas.clientWidth / 2,
|
y: this.viewer?.canvas.clientHeight / 2
|
}
|
|
let centerInfo = this.getMouseInfo(mapCenterPx)
|
|
if (centerInfo.wgs84SurfacePosition) {
|
const {
|
lng,
|
lat,
|
alt
|
} = centerInfo.wgs84SurfacePosition
|
|
// 计算相机到中心点的距离
|
const cameraPosition = Cesium.Cartesian3.fromDegrees(cameraLongitude, cameraLatitude, cameraHeight)
|
const centerPoint = Cesium.Cartesian3.fromDegrees(lng, lat, alt)
|
const distance = Cesium.Cartesian3.distance(cameraPosition, centerPoint)
|
|
smallMpaCenterDistance = distance
|
EventBus.emit('setCameraCurPositionDistance', distance)
|
} else {
|
const cameraPosition = this.viewer?.camera.position
|
const farDistance = this.viewer?.camera.frustum.far // 获取相机的远裁剪面距离
|
const distance = farDistance - Cesium.Cartesian3.magnitude(cameraPosition)
|
smallMpaCenterDistance = distance
|
EventBus.emit('setCameraCurPositionDistance', '')
|
}
|
}
|
|
getMouseInfo (position) {
|
let scene = this.viewer?.scene
|
let surfaceCartesian = undefined
|
let wgs84SurfacePosition = undefined
|
|
if (
|
scene.mode === Cesium.SceneMode.SCENE3D &&
|
!(this.viewer?.terrainProvider instanceof Cesium.EllipsoidTerrainProvider)
|
) {
|
let ray = scene.camera.getPickRay(position)
|
surfaceCartesian = scene.globe.pick(ray, scene)
|
} else {
|
surfaceCartesian = scene.camera.pickEllipsoid(
|
position,
|
Cesium.Ellipsoid.WGS84,
|
)
|
}
|
|
if (surfaceCartesian) {
|
let c = Cesium.Ellipsoid.WGS84.cartesianToCartographic(surfaceCartesian)
|
if (c) {
|
wgs84SurfacePosition = {
|
lng: Cesium.Math.toDegrees(c.longitude),
|
lat: Cesium.Math.toDegrees(c.latitude),
|
alt: c.height,
|
}
|
}
|
}
|
|
return {
|
wgs84SurfacePosition: wgs84SurfacePosition,
|
}
|
}
|
|
getMouseInfoAll (position) {
|
let scene = this.viewer?.scene
|
let target = scene.pick(position)
|
let cartesian = undefined
|
let surfaceCartesian = undefined
|
let wgs84Position = undefined
|
let wgs84SurfacePosition = undefined
|
if (scene.pickPositionSupported) {
|
cartesian = scene.pickPosition(position)
|
}
|
if (cartesian) {
|
let c = Cesium.Ellipsoid.WGS84.cartesianToCartographic(cartesian)
|
if (c) {
|
wgs84Position = {
|
lng: Cesium.Math.toDegrees(c.longitude),
|
lat: Cesium.Math.toDegrees(c.latitude),
|
alt: c.height,
|
}
|
}
|
}
|
if (
|
scene.mode === Cesium.SceneMode.SCENE3D &&
|
!(this.viewer?.terrainProvider instanceof Cesium.EllipsoidTerrainProvider)
|
) {
|
let ray = scene.camera.getPickRay(position)
|
surfaceCartesian = scene.globe.pick(ray, scene)
|
} else {
|
surfaceCartesian = scene.camera.pickEllipsoid(position, Cesium.Ellipsoid.WGS84)
|
}
|
if (surfaceCartesian) {
|
let c = Cesium.Ellipsoid.WGS84.cartesianToCartographic(surfaceCartesian)
|
if (c) {
|
wgs84SurfacePosition = {
|
lng: Cesium.Math.toDegrees(c.longitude),
|
lat: Cesium.Math.toDegrees(c.latitude),
|
alt: c.height,
|
}
|
}
|
}
|
|
return {
|
target: target,
|
windowPosition: position,
|
position: cartesian,
|
wgs84Position: wgs84Position,
|
surfacePosition: surfaceCartesian,
|
wgs84SurfacePosition: wgs84SurfacePosition,
|
}
|
}
|
|
// 左键按下事件
|
pro_left_down (e) {
|
proMoveFlag = !0
|
}
|
|
// 左键抬起事件
|
pro_left_up (e) {
|
proMoveFlag = !1
|
}
|
|
// 鹰眼鼠标移动事件
|
pro_mouse_move (e) {
|
if (proMoveFlag) {
|
this.look3D(e.startPosition, e.endPosition)
|
|
const position = this.viewer?.camera.positionCartographic
|
const lng = Cesium.Math.toDegrees(position.longitude).toFixed(4)
|
const lat = Cesium.Math.toDegrees(position.latitude).toFixed(4)
|
const alt = (position.height / 1000).toFixed(2) // 转换为千米
|
|
// 获取方向
|
const heading = Cesium.Math.toDegrees(this.viewer?.camera.heading).toFixed(2)
|
const pitch = Cesium.Math.toDegrees(this.viewer?.camera.pitch).toFixed(2)
|
const roll = Cesium.Math.toDegrees(this.viewer?.camera.roll).toFixed(2)
|
|
curCameraHpr = {
|
heading,
|
pitch,
|
roll
|
}
|
|
let newHeading = ((heading + 180) % 360) - 180
|
EventBus.emit('PointAirLine-setRoamPoint', {
|
pitch: pitch,
|
heading: newHeading >= 0 ? newHeading - 180 : newHeading + 180
|
})
|
|
this.getCameraCurPositionDistance()
|
}
|
}
|
|
// 鹰眼地图相关事件
|
bindEvent () {
|
this.bindProLeftDown = this.pro_left_down.bind(this)
|
this.bindProLeftUp = this.pro_left_up.bind(this)
|
this.bindProLeftMove = this.pro_mouse_move.bind(this)
|
|
proHandler = new Cesium.ScreenSpaceEventHandler(this.viewer?.scene.canvas)
|
|
proController = this.viewer?.scene.screenSpaceCameraController
|
|
proController.enableRotate = !1
|
proController.enableTilt = !1
|
proController.enableZoom = !1
|
proController.enableLook = !1
|
proController.enableTranslate = !1
|
proMoveFlag = false
|
|
handler.setInputAction(this.bindProLeftDown, Cesium.ScreenSpaceEventType.LEFT_DOWN)
|
handler.setInputAction(this.bindProLeftUp, Cesium.ScreenSpaceEventType.LEFT_UP)
|
handler.setInputAction(this.bindProLeftMove, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
|
}
|
|
unBindEvent = () => {
|
if (proHandler) {
|
handler.removeInputAction(this.bindProLeftDown, Cesium.ScreenSpaceEventType.LEFT_DOWN)
|
handler.removeInputAction(this.bindProLeftUp, Cesium.ScreenSpaceEventType.LEFT_UP)
|
handler.removeInputAction(this.bindProLeftMove, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
|
|
proController = null
|
proHandler = null
|
}
|
}
|
|
look3D (e, t, i = !1) {
|
let r = this.viewer?.camera
|
, a = this.viewer?.scene.globe.ellipsoid
|
, s = i ? -1 : 2
|
, o = a.geodeticSurfaceNormal(r.position)
|
r.look(o, this.getLookAngle(e, t, "x") * s),
|
this.lookVertical(-this.getLookAngle(e, t, "y") * s, o)
|
}
|
|
getLookAngle (e, t, i) {
|
let r, a
|
let s = e[i]
|
, o = t[i]
|
, n = new Cesium.Cartesian2("x" === i ? s : 0, "y" === i ? s : 0)
|
, l = new Cesium.Cartesian2("x" === i ? o : 0, "y" === i ? o : 0)
|
, c = this.viewer?.camera.getPickRay(n)
|
, h = this.viewer?.camera.getPickRay(l)
|
if (!c || !h)
|
return 0
|
this.viewer?.camera.frustum instanceof Cesium.OrthographicFrustum ? (r = c.origin,
|
a = h.origin,
|
Cesium.Cartesian3.add(this.viewer?.camera.direction, r, r),
|
Cesium.Cartesian3.add(this.viewer?.camera.direction, a, a),
|
Cesium.Cartesian3.subtract(r, this.viewer?.camera.position, r),
|
Cesium.Cartesian3.subtract(a, this.viewer?.camera.position, a),
|
Cesium.Cartesian3.normalize(r, r),
|
Cesium.Cartesian3.normalize(a, a)) : (r = c.direction,
|
a = h.direction)
|
let d = Cesium.Cartesian3.dot(r, a)
|
, p = 0
|
return d < 1 && (p = Math.acos(d)),
|
s > o ? -p : p
|
}
|
|
lookVertical (e, t) {
|
let i = this.viewer?.camera
|
, r = this.judgeParallelAndRecalculateAngle(-e, i.direction, t)
|
r && i.look(i.right, r)
|
}
|
|
sg (e, t) {
|
return Cesium.Cartesian3.equalsEpsilon(e, t, Cesium.Math.EPSILON2) ? 1 : Cesium.Cartesian3.equalsEpsilon(e, Cesium.Cartesian3.negate(t, new Cesium.Cartesian3), Cesium.Math.EPSILON2) ? -1 : 0
|
}
|
|
judgeParallelAndRecalculateAngle (e, t, i) {
|
let r = this.sg(t, i)
|
, a = e
|
if (1 === r && a < 0)
|
a = 0
|
else if (-1 === r && a > 0)
|
a = 0
|
else if (0 === r) {
|
let e = Cesium.Cartesian3.dot(t, i)
|
, r = Cesium.Math.acosClamped(-e)
|
a > 0 && a > r && (a = r - Cesium.Math.EPSILON4),
|
r = Cesium.Math.acosClamped(e),
|
a < 0 && -a > r && (a = -r + Cesium.Math.EPSILON4)
|
}
|
return a
|
}
|
|
getEntityById (id) {
|
const pointEntity = this.viewer?.entities.getById(id)
|
return pointEntity
|
}
|
|
removeById (id) {
|
this.viewer?.entities.removeById(id)
|
const pointEntity = this.viewer?.entities.getById(id)
|
if (pointEntity && pointEntity !== undefined) {
|
this.viewer?.entities.remove(pointEntity)
|
}
|
}
|
|
removeAllPoint () {
|
if (this.viewer) {
|
this.viewer?.entities?.removeAll()
|
// viewer?.scene?.primitives?.removeAll();
|
}
|
}
|
|
// 添加entity
|
addCustomImageryProviderDataSource (imageryLayers) {
|
return this.viewer?.imageryLayers.addImageryProvider(imageryLayers)
|
}
|
|
showCustomImageryProviderDataSource (imageryLayers, show) {
|
if (imageryLayers) {
|
imageryLayers.show = show
|
}
|
}
|
|
|
removeAllCustomImageryProviderDataSource (imageryLayers) {
|
if (imageryLayers) {
|
this.viewer?.imageryLayers.remove(imageryLayers)
|
}
|
}
|
}
|