需求:要求将GLTF三维模型放到地图上展示,并且添加各种图标和线进行标注。
用CesiumJS地图工具实现代码如下:
初始化地图:
Cesium.Ion.defaultAccessToken = 'Your token'; const imageryProvider = new Cesium.UrlTemplateImageryProvider({ url: "http://webst01.is.autonavi.com/appmaptile?lang=zh_cn&style=7&x={x}&y={y}&z={z}", tilingScheme: new Cesium.WebMercatorTilingScheme(), fileExtension: 'png', minimumLevel: 0, maximumLevel: 20, }); const viewer = new Cesium.Viewer("cesiumContainer", { imageryProvider: imageryProvider, animation: false, //是否显示动画控件 homeButton: false, //是否显示home键 geocoder: false, //是否显示地名查找控件,如果设置为true,则无法查询 baseLayerPicker: false, //是否显示图层选择控件 timeline: false, //是否显示时间线控件 fullscreenButton: false, //是否全屏显示 scene3DOnly: true, //如果设置为true,则所有几何图形以3D模式绘制以节约GPU资源 infoBox: false, //是否显示点击要素之后显示的信息 sceneModePicker: false, //是否显示投影方式控件 三维/二维 navigationInstructionsInitiallyVisible: false, navigationHelpButton: false, //是否显示帮助信息控件 selectionIndicator: false, //是否显示指示器组件 }); viewer._cesiumWidget._creditContainer.style.display = "none"; //添加文字标注 viewer.imageryLayers.addImageryProvider(new Cesium.WebMapTileServiceImageryProvider({ url: "http://t0.tianditu.com/cva_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=cva&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=dda338577652aefde985448d524c66f4", layer: "tdtAnnoLayer", style: "default", format: "image/jpeg", tileMatrixSetID: "GoogleMapsCompatible", show: false, })); viewer.scene.primitives.add(Cesium.createOsmBuildings()); viewer.scene.highDynamicRange = false; viewer.shadows = false; createGltfModel("../../data/gltf/NewFile.gltf", 117.628293, 23.798717, 0.0, { modelScale: 1, //模型放大倍数 lightColor: new Cesium.Cartesian3(20.0, 20.0, 20.0), //模型的材质亮度 position: { //地图初始化位置,坐标是3维笛卡尔坐标 x: -2707823.1735314624, y: 5173260.303294086, z: 2558159.506208664, }, orientation: { heading: 4.735189003200103, pitch: -0.4113143959917751, roll: 6.2804863005660785, }, }); function createGltfModel(url, lng, lat, height, { modelScale, lightColor, position, orientation } = { modelScale: 1, lightColor: new Cesium.Cartesian3(0, 0, 0), position: { x: 0, y: 0, z: 0 }, orientation: { heading: 0, pitch: 0, roll: 0 } }) { viewer.entities.removeAll(); let ellipsoid = viewer.scene.globe.ellipsoid; let cart3 = ellipsoid.cartographicToCartesian(Cesium.Cartographic.fromDegrees(lng, lat, height)); let modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(cart3); let model = viewer.scene.primitives.add(Cesium.Model.fromGltf({ url, modelMatrix, scale: 1, //放大倍数 lightColor,//gltf模型加载后比较暗,这个起到增大模型材质亮度的作用 })); let initialPosition = new Cesium.Cartesian3(position.x, position.y, position.z); // 相机的位置 let homeCameraView = { destination: initialPosition, // 相机的位置 orientation, }; viewer.scene.camera.setView(homeCameraView); //设置光照(时间) /** * 这里涉及到一个知识点:julianDate与Date之间的转换关系(参考文档:https://blog.csdn.net/xiaotian602574436/article/details/78293637) * js创建的Date对象是北京时间,但是与JulianDate的日期是不同的,cesium中显示的时间与Date日期的时间是差8个小时。北京时间=JulianDate+8 * 这个时间是我调试的结果,模型不至于很暗 * 主要是时间要调到太阳照射到整个模型区域,日期没有多大影响 */ let utc = Cesium.JulianDate.fromDate(new Date("2021/02/20 03:40:00"));//UTC viewer.clock.currentTime = Cesium.JulianDate.addHours(utc, 8, new Cesium.JulianDate()); }
抗锯齿处理:
//抗锯齿处理 { viewer.scene.fxaa = true viewer.scene.postProcessStages.fxaa.enabled = true; let supportsImageRenderingPixelated = viewer.cesiumWidget._supportsImageRenderingPixelated; if (supportsImageRenderingPixelated) { let vtxf_dpr = window.devicePixelRatio; while (vtxf_dpr >= 2.0) { vtxf_dpr /= 2.0; } viewer.resolutionScale = vtxf_dpr; } }
添加图片:
//添加图片 { const iconMarks = [ { id: 'factory1', position: [117.62514171688602, 23.800514411219126, 40], image: '../../data/icons/废气-C.png', }, { id: 'factory2', position: [117.62441672316956, 23.801488899527932, 40], image: '../../data/icons/废气-C.png', }, { id: 'camera1', position: [117.62438331048037, 23.80172986514075, 20], image: '../../data/icons/摄像头-C.png', width: 13, height: 18 }, { id: 'camera2', position: [117.6251283858732, 23.801983468675637, 10], image: '../../data/icons/摄像头-C.png', width: 13, height: 18 } ]; for (const mark of iconMarks) { let width = mark.width || 30; let height = mark.height || 30; viewer.entities.add({ id: mark.id, name: 'mark', position: Cesium.Cartesian3.fromDegrees(...mark.position), billboard: { // 图像地址,URI或Canvas的属性 image: mark.image, // 设置颜色和透明度 // color: Cesium.Color.WHITE.withAlpha(1), // 高度(以像素为单位) height, // 宽度(以像素为单位) width, // 逆时针旋转 rotation: 0, // 大小是否以米为单位 sizeInMeters: false, // 相对于坐标的垂直位置 verticalOrigin: Cesium.VerticalOrigin.CENTER, // 相对于坐标的水平位置 horizontalOrigin: Cesium.HorizontalOrigin.LEFT, // 该属性指定标签在屏幕空间中距此标签原点的像素偏移量 pixelOffset: new Cesium.Cartesian2(-width / 2, -height / 2), // 应用于图像的统一比例。比例大于会1.0放大标签,而比例小于会1.0缩小标签。 scale: 1.0, // 是否显示 show: true } }); } }
添加线:
//添加线 { const markLines = [ { id: 'line1', positions: [[117.62514171688602, 23.800514411219126, 0], [117.62514171688602, 23.800514411219126, 40]], }, { id: 'line2', positions: [[117.62441672316956, 23.801488899527932, 0], [117.62441672316956, 23.801488899527932, 40]], }, { id: 'line3', positions: [[117.62441672316956, 23.801488899527932, 20], [117.62438331048037, 23.80172986514075, 20], [117.6251283858732, 23.801983468675637, 10], [117.62516689671625, 23.802117335288763, 10]] } ]; for (const line of markLines) { let polyline = new Cesium.PolylineGraphics(); polyline.material = new Cesium.ColorMaterialProperty(new Cesium.Color(0.18, 0.67, 1, 1)); polyline.width = new Cesium.ConstantProperty(2); polyline.arcType = new Cesium.ConstantProperty( Cesium.ArcType.NONE ); let coordArr = line.positions.map(e => Cesium.Cartesian3.fromDegrees(...e)); console.log('coordArr', coordArr); polyline.positions = new Cesium.ConstantProperty(coordArr); let entity = new Cesium.Entity({ id: line.id, show: true, polyline, name: 'line', }); viewer.entities.add(entity); } }
添加事件:
//事件 { let handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas); handler.setInputAction(function (evt) { let pick = viewer.scene.pick(evt.position); // //选中某模型 pick选中的对象 if (pick && pick.id) { alert(pick.id._id) } let cartesian = viewer.camera.pickEllipsoid(evt.position, viewer.scene.globe.ellipsoid); let cartographic = Cesium.Cartographic.fromCartesian(cartesian); let lng = Cesium.Math.toDegrees(cartographic.longitude);//经度值 let lat = Cesium.Math.toDegrees(cartographic.latitude);//纬度值 let mapPosition = { x: lng, y: lat, z: cartographic.height };//cartographic.height的值始终为零。 // console.log('mapPosition: ', mapPosition); console.log('lng: ', lng, 'lat: ', lat); }, Cesium.ScreenSpaceEventType.LEFT_CLICK); handler.setInputAction(evt => { let pick = viewer.scene.pick(evt.endPosition); // //选中某模型 pick选中的对象 if (pick && pick.id && pick.id._name === 'mark') { // console.log('pick', pick.id._id); viewer._container.style.cursor = 'pointer'; } else { viewer._container.style.cursor = ''; } }, Cesium.ScreenSpaceEventType.MOUSE_MOVE) }
效果: