import config from '../config'; import LayerMgr3D from './layermgr3d'; import BaseMap from '../basemap'; import Demo3D from './demo' import { ToolManager } from './ToolManager3D'; const { Cesium } = window; function __prepare_ces(dispatch) { Cesium.CesiumWidget.prototype.showErrorPanel = function (title) { dispatch && dispatch.map.setMode('2d'); if (title && title.indexOf('constructing') >= 0) { alert('无法初始化三维场景,如果一直出现此问题,请尝试下载最新的chrome浏览器'); } else { alert('三维场景渲染出现问题'); } }; } /*根据camera高度近似计算当前层级*/ const heightToZoom = ( height) => { var A = 40487.57; var B = 0.00007096758; var c = 91610.74; var D = -40467.74; return Math.round(D+(A-D)/(1+Math.pow( height/c,B)) ); } /** * OL 封装 */ export default class Map3D extends BaseMap { constructor({ divid, dispatch }) { super(); this.dispatch = dispatch; this.divid = divid; // div element id this._map = null; // openlayers map obj this.layerMgr = null; this.toolMgr = null; this.demo = null; // @ts-ignore const open = XMLHttpRequest.prototype.open; // @ts-ignore XMLHttpRequest.prototype.open = function (method, url, ...rest) { if (url.startsWith('http://res3dstatic7')) { url = url.replaceAll('+', '%2B'); } return open.call(this, method, url, ...rest); }; } /** * 初始化地图、图层 */ async init() { __prepare_ces(this.dispatch); // const tiandiKey = "efc861f25f96dc6e5f884f0403ebfefd"; //天地图key,官网申请 // const baseUrl = "https://{s}.tianditu.gov.cn";//'https://t{0-7}.tianditu.gov.cn'; const viewer = new Cesium.Viewer(this.divid, { // terrain: Cesium.Terrain.fromWorldTerrain({ // requestVertexNormals: true, // }), terrainProvider: await Cesium.CesiumTerrainProvider.fromUrl("/shzh/mapres/terrain",{ requestVertexNormals: true }), }); viewer.scene.screenSpaceCameraController.tiltEventTypes = [ Cesium.CameraEventType.RIGHT_DRAG, Cesium.CameraEventType.PINCH, { eventType: Cesium.CameraEventType.LEFT_DRAG, modifier: Cesium.KeyboardEventModifier.CTRL }, { eventType: Cesium.CameraEventType.RIGHT_DRAG, modifier: Cesium.KeyboardEventModifier.CTRL } ]; viewer.scene.screenSpaceCameraController.zoomEventTypes = [ Cesium.CameraEventType.MIDDLE_DRAG, Cesium.CameraEventType.WHEEL, Cesium.CameraEventType.PINCH ]; viewer.camera.setView({ destination: Cesium.Cartesian3.fromDegrees(114.763746000,31.482518000,1000), orientation: { // heading: Cesium.Math.toRadians(0), pitch: Cesium.Math.toRadians(-35.0), roll: 0.0 } }); } coordinateToPixel(lgtd, lttd, elev) { // const pt = Cesium.Cartesian3.fromDegrees(lgtd, lttd, elev); // const result = Cesium.SceneTransforms. // // wgs84ToWindowCoordinates // worldToWindowCoordinates // ( // this._map.scene, pt); // if (!result) { // return null // } // return [result.x, result.y, pt.x, pt.y, pt.z]; } /** * 组件卸载时,需要销毁map对象 */ destroy() { // console.log('##############destroy##############'); // if (!this.layerMgr) { // return; // } // this.layerMgr.destroy(); // if (this._map) { // this._map.destroy(); // this._map = null; // } } /** * get layer obj */ getLayer(name) { // return this.layerMgr.getLayer(name); } zoomTo(cameraTarget) { // if (cameraTarget.center) { // this._map.camera.flyToBoundingSphere(new Cesium.BoundingSphere( // Cesium.Cartesian3.fromDegrees(cameraTarget.center[0], cameraTarget.center[1], cameraTarget.center[2] || 0), // 800) // ); // } else if (cameraTarget.bound) { // const b = cameraTarget.bound; // const p1 = Cesium.Cartesian3.fromDegrees(...b[0]); // const p2 = Cesium.Cartesian3.fromDegrees(...b[1]); // this._map.camera.flyToBoundingSphere(Cesium.BoundingSphere.fromPoints([p1, p2])); // } } }