| | |
| | | --> |
| | | <template> |
| | | <div class="map-container"> |
| | | <div class="viewer-container" id="viewer-container"> |
| | | <div class="viewer-container ztzf-cesium" id="viewer-container"> |
| | | <div class="content"> |
| | | <slot name="content"></slot> |
| | | </div> |
| | |
| | | </template> |
| | | |
| | | <script setup> |
| | | import * as Cesium from 'cesium' |
| | | import { Cartesian3, Terrain, Viewer } from 'cesium' |
| | | import { PublicCesium } from '@/utils/cesium/publicCesium' |
| | | import ImageTrailMaterial from '@/utils/cesium/ImageTrailMaterial' |
| | | import { flyVisual } from '@/utils/cesium/mapUtil' |
| | | import * as turf from '@turf/turf' |
| | | |
| | | import { nextTick, onMounted, onUnmounted } from 'vue' |
| | |
| | | |
| | | import startPng from '@/assets/map_images/Startingpointicon.png' |
| | | import endPng from '@/assets/map_images/EndPointicon.png' |
| | | import rwqfdImg from '@/assets/images/task/arrow-right-blue.png' |
| | | import newNumPoint from '@/assets/images/task/custom-point.png' |
| | | |
| | | window.$viewer = null |
| | | window.$Cesium = null |
| | |
| | | |
| | | async function initMap () { |
| | | if (window.$viewer) return |
| | | |
| | | await DC.ready({ |
| | | // Cesium: Cesium, |
| | | baseUrl: `${VITE_APP_BASE}/libs/dc-sdk/resources/`, |
| | | }) |
| | | |
| | | window.$Cesium = DC.getLib('Cesium') |
| | | |
| | | // 天地图地图 |
| | | const imageryProvider_standZh = new window.$Cesium.UrlTemplateImageryProvider({ |
| | | url: 'https://t{s}.tianditu.gov.cn/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=e45274b0235bb913eceb393aabbf9c9c', |
| | | subdomains: ['0', '1', '2', '3', '4', '5', '6', '7'], |
| | | maximumLevel: 18, |
| | | credit: 'stand_zj', |
| | | }) |
| | | const imageryProvider_stand = new window.$Cesium.UrlTemplateImageryProvider({ |
| | | url: 'https://t{s}.tianditu.gov.cn/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=e45274b0235bb913eceb393aabbf9c9c', |
| | | subdomains: ['0', '1', '2', '3', '4', '5', '6', '7'], |
| | | // format: 'image/jpeg', |
| | | // show: true, |
| | | maximumLevel: 18, |
| | | credit: 'stand_tc', |
| | | }) |
| | | |
| | | window.$viewer = new DC.Viewer('viewer-container', { |
| | | "sceneMode": 2 //1: 2.5D,2: 2D,3: 3D |
| | | }) |
| | | |
| | | window.$viewer.locationBar.enable = false |
| | | |
| | | window.$viewer?.zoomToPosition(new DC.Position(115.892151, 28.676493, 1000000, 0, -90, 0)) |
| | | |
| | | window.$viewer?.imageryLayers.addImageryProvider(imageryProvider_stand) |
| | | window.$viewer?.imageryLayers.addImageryProvider(imageryProvider_standZh) |
| | | |
| | | pointLayer = new DC.VectorLayer('pointLayer') |
| | | window.$viewer?.addLayer(pointLayer) |
| | | polylineLayer = new DC.VectorLayer('polylineLayer') |
| | | window.$viewer?.addLayer(polylineLayer) |
| | | pointHtmlLayer = new DC.HtmlLayer('pointHtmlLayer') |
| | | window.$viewer?.addLayer(pointHtmlLayer) |
| | | |
| | | window.$Cesium = new PublicCesium({ dom: 'viewer-container' }).getViewer() |
| | | isViewerReady.value = true |
| | | } |
| | | |
| | |
| | | * @param data 数据 数据格式 [lng, lat] |
| | | */ |
| | | function addPoint (data) { |
| | | if (pointLayer) pointLayer.clear() |
| | | |
| | | const [lng, lat] = data |
| | | |
| | | if (!lng || !lat) return |
| | | |
| | | let point = new DC.Point(new DC.Position(lng, lat)) |
| | | pointLayer.addOverlay(point) |
| | | window.$Cesium.entities.add({ |
| | | position: Cartesian3.fromDegrees(lng, lat), |
| | | point: { |
| | | pixelSize: 10, |
| | | color: Cesium.Color.BLUE, |
| | | outlineColor: Cesium.Color.WHITE, |
| | | outlineWidth: 2 |
| | | } |
| | | }) |
| | | |
| | | window.$viewer?.zoomTo(pointLayer) |
| | | // 定位到点位 |
| | | const points = [[lng, lat]] // 确保格式为二维数组 |
| | | flyVisual(points, window.$Cesium, 4) |
| | | } |
| | | |
| | | /** |
| | |
| | | const positionStr = data.map(item => { |
| | | const [lng, lat] = item |
| | | |
| | | return `${lng}, ${lat}` |
| | | }).join(';') |
| | | |
| | | let polyline = new DC.Polyline(positionStr) |
| | | polyline.setStyle({ |
| | | width: 4, |
| | | material: new DC.PolylineTrailMaterialProperty({ |
| | | color: DC.Color.DEEPSKYBLUE, |
| | | speed: 10 |
| | | }), |
| | | clampToGround: true |
| | | return Cartesian3.fromDegrees(Number(lng), Number(lat)) |
| | | }) |
| | | polylineLayer.addOverlay(polyline) |
| | | |
| | | |
| | | data.forEach((item, index) => { |
| | | const [lng, lat] = item |
| | | let position = new DC.Position(lng, lat) |
| | | |
| | | console.log(lng, lat) |
| | | let billboard = null |
| | | |
| | | if (index === 0) { |
| | | billboard = new DC.Billboard(position, startPng) |
| | | } |
| | | |
| | | if (index === data.length - 1) { |
| | | billboard = new DC.Billboard(position, endPng) |
| | | } |
| | | |
| | | |
| | | billboard && (billboard.size = [20, 20]) |
| | | billboard && (billboard.setStyle({ |
| | | "pixelOffset": { "x": 0, "y": -8 } |
| | | })) |
| | | billboard && polylineLayer.addOverlay(billboard) |
| | | |
| | | if (index !== 0 && index !== data.length - 1) { |
| | | let divIcon = new DC.DivIcon( |
| | | position, |
| | | `<div class="point-icon-box">${index}</div>` |
| | | ) |
| | | pointHtmlLayer.addOverlay(divIcon) |
| | | } |
| | | }) |
| | | |
| | | const line = turf.lineString(positionStr.split(';').map(i => i.split(','))) |
| | | const bbox = turf.bbox(line) |
| | | const bboxPolygon = turf.bboxPolygon(bbox) |
| | | const scaledPolygon = turf.transformScale(bboxPolygon, 3) |
| | | const newBbox = turf.bbox(scaledPolygon) |
| | | |
| | | window.$viewer?.flyToBounds( |
| | | newBbox, |
| | | { heading: 0, pitch: -90, roll: 0 }, |
| | | () => { }, |
| | | 0 |
| | | ) |
| | | // 路径线 |
| | | window.$Cesium.entities.add({ |
| | | id: `polyline`, |
| | | polyline: { |
| | | width: 4, |
| | | positions: positionStr, |
| | | material: new ImageTrailMaterial({ |
| | | image: rwqfdImg, // 使用导入的图片 |
| | | repeat: { |
| | | x: 10, // 调整图片重复次数 |
| | | y: 1 |
| | | }, |
| | | speed: 10, // 调整速度 |
| | | color: Cesium.Color.WHITE // 可以调整图片的颜色混合 |
| | | }), |
| | | clampToGround: false, |
| | | }, |
| | | }) |
| | | positionStr.forEach((point, index) => { |
| | | let setting = {} |
| | | if (index === 0) { |
| | | setting = { |
| | | position: point, |
| | | billboard: { |
| | | image: startPng, |
| | | outlineWidth: 0, |
| | | width: 30, |
| | | height: 30, |
| | | scale: 1.0, |
| | | }, |
| | | } |
| | | } else if (index === positionStr.length - 1) { |
| | | setting = { |
| | | position: point, |
| | | id: `point_${index}`, |
| | | billboard: { |
| | | image: endPng, |
| | | outlineWidth: 0, |
| | | width: 30, |
| | | height: 30, |
| | | scale: 1.0, |
| | | verticalOrigin: Cesium.VerticalOrigin.TOP, // 添加这行确保图标正确显示 |
| | | pixelOffset: new Cesium.Cartesian2(0, -20), // 根据需要调整偏移量 |
| | | }, |
| | | } |
| | | } else { |
| | | setting = { |
| | | position: point, |
| | | id: `point_${index}`, |
| | | label: { |
| | | text: `${index}`, |
| | | font: 'bold 14px serif', |
| | | fillColor: Cesium.Color.WHITE, |
| | | pixelOffset: new Cesium.Cartesian2(1, 0), // 根据需要调整偏移量 |
| | | eyeOffset: new Cesium.Cartesian3(0, 0, -10), // 使标签在点的上方 |
| | | }, |
| | | billboard: { |
| | | image: new Cesium.ConstantProperty(newNumPoint), |
| | | width: 30, |
| | | height: 30, |
| | | }, |
| | | offset: new Cesium.Cartesian2(10, 30), |
| | | } |
| | | } |
| | | window.$Cesium.entities.add(setting) |
| | | }) |
| | | // 定位到点位 |
| | | flyVisual(data, window.$Cesium, 4) |
| | | } |
| | | |
| | | onMounted(() => { |
| | |
| | | window.$viewer?.entities.removeAll() |
| | | window.$viewer?.imageryLayers.removeAll() |
| | | window.$viewer?.dataSources.removeAll() |
| | | let gl = window.$viewer.scene.context._originalGLContext |
| | | gl.canvas.width = 1 |
| | | gl.canvas.height = 1 |
| | | // let gl = window.$viewer.scene.context._originalGLContext |
| | | // gl.canvas.width = 1 |
| | | // gl.canvas.height = 1 |
| | | |
| | | window.$viewer && window.$viewer.setTerrain() |
| | | window.$viewer && window.$viewer.destroy() |