import Vue from "vue"; import coord from "@/components/poplayer/coord"; import store from "@/utils/store"; import mapData from "../../../static/mapData"; let PoiLayerConstructor = Vue.extend(coord); window.rpc = { timeout: 500, url: "http://localhost:8999/RPC2", method: "get_statue", divPoint: null, instance: null, pathLayer: null, position: null, marksIndex: null, pitchValue: null, //消息对接 initMessage() { var that = this; window.addEventListener("message", res => { if (res.data.status === "ok") { console.log(res.data.params, "当前事件数据"); // that.setLocation(); } }); }, //地图复位 setMapRest() { const data = mapData.mapRest; sgworld.Navigate.flyToPointsInterest({ destination: { "x": data.lng, "y": data.lat, "z": data.alt }, orientation: { heading: data.heading, pitch: data.pitch, roll: data.roll, }, duration: 5, //飞行时间8s }); }, //点击获取坐标 clickToCoordinates() { const handlerClick = new Cesium.ScreenSpaceEventHandler(Viewer.scene.canvas); handlerClick.setInputAction((event) => { let cartesian = Viewer.camera.pickEllipsoid(event.position, Viewer.scene.globe.ellipsoid); if (cartesian == undefined) { console.log('没有获取到坐标') } else { // 空间坐标转世界坐标(弧度) let cartographic = Cesium.Cartographic.fromCartesian(cartesian); // 弧度转为角度(经纬度) let lon = Cesium.Math.toDegrees(cartographic.longitude); // 经度值 let lat = Cesium.Math.toDegrees(cartographic.latitude); // 纬度值 console.log('经纬度是:', { x: lon, y: lat }); } }, Cesium.ScreenSpaceEventType.LEFT_CLICK); }, //定位弹窗 setLocation() { var data = mapData.pointPop; var objdata = { POITYPE: data.text, SSDW: data.text, LXR: data.text, DIZHI: res, lat: data.lat, lon: data.lng }; //添加定位弹窗 this.setAddPointLocation(objdata); // //添加路径线 // this.setShowPathLine(marks); // //飞行漫游 // window.Viewer.scene.camera.flyTo({ // destination: Cesium.Cartesian3.fromDegrees( // marks[0].lng, // marks[0].lat, // marks[0].height // ), //定位坐标点,建议使用谷歌地球坐标位置无偏差 // duration: 7 //定位的时间间隔 // }); // this.marksIndex = 1; // this.pitchValue = -20; // setTimeout(() => { // this.setflyExtent(marks); // }, 7000); }, setAddPointLocation(res) { // 清除方法 this.setClearPathlayer(); store.setlocalLayerShowAction(false); store.setlocalLayerListAction({}); store.setlocalLayerShowAction(true); store.setlocalLayerListAction(res); this.instance = new PoiLayerConstructor({ data: { list: res } }); this.instance.$mount(); this.divPoint = sgworld.Creator.createDivPoint( "", { lon: res.lon, lat: res.lat, height: 50 }, { type: "custom", offset: ["c", 100], description: this.instance.$el, near: 0, far: 100000 } ); this.position = window.Viewer.entities.add({ name: "FLYToMap", position: Cesium.Cartesian3.fromDegrees(objdata.lon, objdata.lat), point: { pixelSize: 1, color: Cesium.Color.WHITE.withAlpha(0), outlineColor: Cesium.Color.WHITE.withAlpha(0.9), outlineWidth: 1 } }); window.Viewer.flyTo(this.position, { offset: { heading: Cesium.Math.toRadians(0.0), pitch: Cesium.Math.toRadians(-45), range: 600 } }); setTimeout(() => { window.Viewer.entities.remove(this.position) }, 1000); }, setflyExtent(res) { // 相机看点的角度,如果大于0那么则是从地底往上看,所以要为负值 var pitch = Cesium.Math.toRadians(this.pitchValue); // 时间间隔2秒钟 this.setExtentTime(res[this.marksIndex].flytime); var Exection = () => { var preIndex = this.marksIndex - 1; if (this.marksIndex == 0) { preIndex = res.length - 1; } var heading = this.bearing( res[preIndex].lat, res[preIndex].lng, res[this.marksIndex].lat, res[this.marksIndex].lng ); heading = Cesium.Math.toRadians(heading); // 当前已经过去的时间,单位s var delTime = Cesium.JulianDate.secondsDifference( window.Viewer.clock.currentTime, window.Viewer.clock.startTime ); var originLat = this.marksIndex == 0 ? res[res.length - 1].lat : res[this.marksIndex - 1].lat; var originLng = this.marksIndex == 0 ? res[res.length - 1].lng : res[this.marksIndex - 1].lng; var endPosition = Cesium.Cartesian3.fromDegrees( originLng + ((res[this.marksIndex].lng - originLng) / res[this.marksIndex].flytime) * delTime, originLat + ((res[this.marksIndex].lat - originLat) / res[this.marksIndex].flytime) * delTime, res[this.marksIndex].height ); console.log(1, pitch) window.Viewer.scene.camera.setView({ destination: endPosition, orientation: { heading: heading, pitch: Cesium.Math.toRadians(-5), }, }); if ( Cesium.JulianDate.compare( window.Viewer.clock.currentTime, window.Viewer.clock.stopTime ) >= 0 ) { window.Viewer.clock.onTick.removeEventListener(Exection); this.changeCameraHeading(res); } } window.Viewer.clock.onTick.addEventListener(Exection); }, setPointFly() { var alll = { lng: 116.802948, lat: 36.550064, height: 100, flytime: 15 } window.sgworld.Analysis.setPointFly({ position: Cesium.Cartesian3.fromDegrees(alll.lng, alll.lat, 50), hideImage: false }) }, changeCameraHeading(res) { var nextIndex = this.marksIndex + 1; if (this.marksIndex == res.length - 1) { // 漫游完成 this.setPointFly(res); return } // 计算两点之间的方向 var heading = this.bearing( res[this.marksIndex].lat, res[this.marksIndex].lng, res[nextIndex].lat, res[nextIndex].lng ); // 相机看点的角度,如果大于0那么则是从地底往上看,所以要为负值 var pitch = Cesium.Math.toRadians(this.pitchValue); // 给定飞行一周所需时间,比如10s, 那么每秒转动度数 var angle = (heading - Cesium.Math.toDegrees(window.Viewer.camera.heading)) / 2; // 时间间隔2秒钟 this.setExtentTime(2); // 相机的当前heading var initialHeading = window.Viewer.camera.heading; var Exection = () => { // 当前已经过去的时间,单位s var delTime = Cesium.JulianDate.secondsDifference( window.Viewer.clock.currentTime, window.Viewer.clock.startTime ); var heading = Cesium.Math.toRadians(delTime * angle) + initialHeading; window.Viewer.scene.camera.setView({ orientation: { heading: heading, pitch: Cesium.Math.toRadians(-5), }, }); if ( Cesium.JulianDate.compare( window.Viewer.clock.currentTime, window.Viewer.clock.stopTime ) >= 0 ) { window.Viewer.clock.onTick.removeEventListener(Exection); this.marksIndex = ++this.marksIndex >= res.length ? 0 : this.marksIndex; this.setflyExtent(res); } }; window.Viewer.clock.onTick.addEventListener(Exection); }, bearing(startLat, startLng, destLat, destLng) { startLat = this.toRadians(startLat); startLng = this.toRadians(startLng); destLat = this.toRadians(destLat); destLng = this.toRadians(destLng); let y = Math.sin(destLng - startLng) * Math.cos(destLat); let x = Math.cos(startLat) * Math.sin(destLat) - Math.sin(startLat) * Math.cos(destLat) * Math.cos(destLng - startLng); let brng = Math.atan2(y, x); let brngDgr = this.toDegrees(brng); return (brngDgr + 360) % 360; }, toRadians(degrees) { return (degrees * Math.PI) / 180; }, toDegrees(radians) { return (radians * 180) / Math.PI; }, setExtentTime(time) { var startTime = Cesium.JulianDate.fromDate(new Date()); var stopTime = Cesium.JulianDate.addSeconds( startTime, time, new Cesium.JulianDate() ); window.Viewer.clock.startTime = startTime.clone(); // 开始时间 window.Viewer.clock.stopTime = stopTime.clone(); // 结速时间 window.Viewer.clock.currentTime = startTime.clone(); // 当前时间 window.Viewer.clock.clockRange = Cesium.ClockRange.CLAMPED; // 行为方式 window.Viewer.clock.clockStep = Cesium.ClockStep.SYSTEM_CLOCK; // 时钟设置为当前系统时间; 忽略所有其他设置。 }, setShowPathLine(res) { var geom = []; for (var i in res) { geom.push([res[i].lng, res[i].lat]); } var url = { type: "FeatureCollection", features: [ { type: "Feature", geometry: { type: "LineString", coordinates: geom } } ] }; this.pathLayer = window.sgworld.Creator.createPathLayer({ url: url, color: "#0033FF", //线的颜色 width: 4.0, //线的宽度 pointColor: "#FFFFFF", //移动点的颜色 speed: 5, far: 50000 }); }, setClearPathlayer() { if (this.divPoint) { this.divPoint && this.divPoint.deleteObject(); this.divPoint = null; } if (this.instance) { this.instance && this.instance.$destroy(); this.instance = null; } if (this.pathLayer) { this.pathLayer && this.pathLayer.deleteObject(); } if (this.position) { window.Viewer.entities.remove(this.position); this.position = null; } } }; export default rpc;