一、初始化地图
<template>
<div class="cesium_map">
<div id="cesiumContainer"></div>
</div>
</template>
<script setup>
import { reactive, ref, onMounted } from "vue";
import { Line } from "../../utils/Model";
// ---------------------------<<数据>>---------------------------
const me = reactive({
point: null,
});
// 点位数组
const pointArr = reactive([
{
lon: 110.645025,
lat: 22.03158,
alt: 5000,
time: null,
},
{
lon: 110.645025,
lat: 22.16643590658971,
alt: 5000,
time: null,
},
{
lon: 110.7480626299661,
lat: 22.261761037919744,
alt: 5000,
time: null,
},
{
lon: 110.8937798546077,
lat: 22.261761037919744,
alt: 5000,
time: null,
},
{
lon: 110.99674745330135,
lat: 22.16643590658971,
alt: 5000,
time: null,
},
{
lon: 110.99674745330135,
lat: 22.03158,
alt: 5000,
time: null,
},
{
lon: 110.8937798546077,
lat: 21.936190428417305,
alt: 5000,
time: null,
},
{
lon: 110.7483984431316,
lat: 21.936190428417305,
alt: 5000,
time: null,
},
{
lon: 110.645025,
lat: 22.03158,
alt: 5000,
time: null,
},
]);
// ---------------------------<<函数>>---------------------------
// 初始化地图
const initMap = () => {
// 在线地图token
Cesium.Ion.defaultAccessToken = "map_token";
// 在线地图
let imageryProvider = new Cesium.ArcGisMapServerImageryProvider({
url: "map_url",
});
window.viewer = new Cesium.Viewer("cesiumContainer", {
geocoder: false, // 右上角 搜索
homeButton: false, // 右上角 Home
sceneModePicker: false, // 右上角 2D/3D切换
baseLayerPicker: false, // 右上角 地形
navigationHelpButton: false, // 右上角 Help
animation: false, // 左下角 圆盘动画控件
timeline: false, // 时间轴
fullscreenButton: false, // 右下角 全屏控件
vrButton: false, // 如果设置为true,将创建VRButton小部件。
scene3DOnly: false, // 每个几何实例仅以3D渲染以节省GPU内存
infoBox: false, // 隐藏点击要素后的提示信息
imageryProvider: imageryProvider, // 地图地址
shouldAnimate: true,
});
viewer._cesiumWidget._creditContainer.style.display = "none";
viewer.scene.globe.enableLighting = false;
viewer.shadows = false;
viewer.scene.globe.enableLighting = false;
Cesium.Camera.DEFAULT_VIEW_FACTOR = 0.2; // 摄像机到地图的距离放大倍数
viewer.camera.flyHome(0);
// // 调整画面精细度
viewer.resolutionScale = 1; //默认值为1.0
viewer.scene.backgroundColor = Cesium.Color.TRANSPARENT;
viewer.scene.globe.baseColor = Cesium.Color.TRANSPARENT;
// //是否开启抗锯齿
viewer.scene.fxaa = false;
viewer.scene.postProcessStages.fxaa.enabled = false;
viewer.scene.globe.showGroundAtmosphere = true;
// 设置相机位置
viewer.scene.camera.setView({
destination: Cesium.Cartesian3.fromDegrees(
110.82474250408677,
22.095133670052277,
80000
),
orientation: {
heading: Cesium.Math.toRadians(0),
pitch: Cesium.Math.toRadians(-90),
roll: 0.0,
},
});
//取消双击事件
viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(
Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK
);
};
// 初始化模型
const initModel = () => {
// 请查看 “步骤三、步骤四” 使用示例
};
// 初始化模型类
const initModelClass = () => {
me.point = new Line(viewer, Cesium);
};
// ---------------------------<<执行>>---------------------------
// 挂载后生命周期
onMounted(() => {
initMap();
initModelClass();
initModel();
});
</script>
<style lang="less" scoped>
.cesium_map {
width: 100%;
height: 100%;
#cesiumContainer {
width: 100%;
height: 100%;
}
}
</style>
二、封装文件
标记有***注释的参数为必传参数;
其他为可选参数;
export class Line {
// 构造函数
constructor(viewer, Cesium) {
this.viewer = viewer
this.Cesium = Cesium
}
// 坐标转换
// Cesium.Cartesian3.fromDegreesArrayHeights:[经,纬,高,经,纬,高,...]
// Cesium.Cartesian3.fromDegreesArray:[经,纬,经,纬,...]
// Cesium.Cartesian3.fromDegrees:[经,纬]
// entitiesLine
entitiesLine(option) {
let {
id, // 线id***
positions, // 线坐标***
material, // 线材质***
name = 'entitiesLine',
width = 2, // 线宽度
clampToGround = true, // 是否贴地
zIndex = 999, // 线渲染层级
ddc = { // 显示区间
start: 0,
end: 150000000
},
show = true, // 是否显示
} = option
this.viewer.entities.add({
id, // 线条id
name, // 线条名称
polyline: {
positions, // 参数依次为[经度1, 纬度1, 高度1, 经度2, 纬度2, 高度2]
width, // 宽度
material, // 线的颜色
clampToGround, // 是否贴地
zIndex, // 线的顺序,仅当`clampToGround`为true并且支持地形上的折线时才有效。
vertexFormat: Cesium.PolylineMaterialAppearance.VERTEX_FORMAT,
distanceDisplayCondition: new this.Cesium.DistanceDisplayCondition(
ddc.start,
ddc.end
), // 显示在距相机的距离处的属性,多少区间内是可以显示的
show // 是否显示
},
});
}
// primitivesLine
primitivesLine(option) {
let {
positions, // 线坐标***
material, // 线材质***
width = 5, // 线宽度
releaseGeometry = true, // 是否自动清空geometryInstances配置
geometryInstances = new Cesium.GeometryInstance({
id: "fluorescence", // 线条id,此处id可以重复添加
name: "fluorescence", // 线条name
}), // 配置实例信息
ddc = {
start: 0,
end: 150000000
}, // 显示区间
show = true // 是否显示
} = option
var polyLines = this.viewer.scene.primitives.add(
new Cesium.PolylineCollection()
);
polyLines.add({
positions,
width,
material,
geometryInstances,
releaseGeometryInstances: releaseGeometry, // 默认为true,自动清空geometryInstances配置
distanceDisplayCondition: new this.Cesium.DistanceDisplayCondition(
ddc.start,
ddc.end
), // 显示在距相机的距离处的属性,多少区间内是可以显示的
show
});
}
}
三、entitiesLine示例
1、实线(3D)
(1)全参数代码
const initModel = () => {
let temp = ref([]);
for (let i = 0; i < pointArr.length; i++) {
temp.value.push(pointArr[i].lon, pointArr[i].lat, pointArr[i].alt);
}
me.line.entitiesLine({
id: "solidLine", // 线条id***
name: "solidLine", // 线条name
// 参数依次为[经度1, 纬度1, 高度1, 经度2, 纬度2, 高度2]
positions: Cesium.Cartesian3.fromDegreesArrayHeights(temp.value), // 线条坐标***
width: 2,
material: Cesium.Color.fromCssColorString("tomato"), // 线条材质:通过材质控制线条类型***
clampToGround: false, // 是否贴地
zIndex: 1, // clampToGround = true才生效
ddc: {
start: 0,
end: 150000000,
}, //显示在距相机的距离处的属性,多少区间内是可以显示的
show: true, // 是否显示线条
});
}
(2)仅必传参数代码
const initModel = () => {
let temp = ref([]);
for (let i = 0; i < pointArr.length; i++) {
temp.value.push(pointArr[i].lon, pointArr[i].lat, pointArr[i].alt);
}
me.line.entitiesLine({
id: "solidLine", // 线条id***
// 参数依次为[经度1, 纬度1, 高度1, 经度2, 纬度2, 高度2]
positions: Cesium.Cartesian3.fromDegreesArrayHeights(temp.value), // 线条坐标***
material: Cesium.Color.fromCssColorString("tomato"), // 线条材质:通过材质控制线条类型***
});
}
(3)效果
2、虚线(3D)
(1)全参数代码
const initModel = () => {
me.line.entitiesLine({
id: "dashedLine", // 线条id***
name: "dashedLine", // 线条name
positions: Cesium.Cartesian3.fromDegreesArrayHeights([
110.82474250408677, 22.095133670052277, 0, 110.645025, 22.03158, 5000,
]), // 线条坐标***
material: new Cesium.PolylineDashMaterialProperty({
color: Cesium.Color.YELLOW, // 短划线颜色
dashLength: 20, // 短划线长度
}), // 线条材质:通过材质控制线条类型***
width: 2, // 线条宽度,default:2
clampToGround: false, // 是否贴地,default:false
zIndex: 1, // 线条层级,clampToGround = true才生效,default:999
ddc: {
start: 0,
end: 150000000,
}, //显示在距相机的距离处的属性,多少区间内是可以显示的,default:{ start: 0,end: 150000000,}
show: true, // 是否显示线条,default:true
});
}
(2)仅必传参数代码
const initModel = () => {
me.line.entitiesLine({
id: "dashedLine", // 线条id***
positions: Cesium.Cartesian3.fromDegreesArrayHeights([
110.82474250408677, 22.095133670052277, 0, 110.645025, 22.03158, 5000,
]), // 线条坐标***
material: new Cesium.PolylineDashMaterialProperty({
color: Cesium.Color.YELLOW, // 短划线颜色
dashLength: 20, // 短划线长度
}), // 线条材质:通过材质控制线条类型***
});
}
(3)效果
3、直线(2D)
(1)全参数代码
const initModel = () => {
me.line.entitiesLine({
id: "straightLine", // 线条id***
name: "straightLine", // 线条name
positions: Cesium.Cartesian3.fromDegreesArray([
110.82474250408677, 22.095133670052277, 110.645025, 22.16643590658971,
]),
// 线条坐标***
material: Cesium.Color.fromCssColorString("yellow"), // 线条材质:通过材质控制线条类型***,必传
width: 2, // 线条宽度,default:2
ddc: {
start: 0,
end: 150000000,
}, //显示在距相机的距离处的属性,多少区间内是可以显示的,default:{ start: 0,end: 150000000,}
show: true, // 是否显示线条,default:true
});
}
(2)仅必传参数代码
const initModel = () => {
me.line.entitiesLine({
id: "straightLine", // 线条id***
positions: Cesium.Cartesian3.fromDegreesArray([
110.82474250408677, 22.095133670052277, 110.645025, 22.16643590658971,
]),
// 线条坐标***
material: Cesium.Color.fromCssColorString("yellow"), // 线条材质:通过材质控制线条类型***
});
}
(3)效果
4、虚线(2D)
(1)全参数代码
const initModel = () => {
me.line.entitiesLine({
id: "groundDashedLine", // 线条id***
name: "groundDashedLine", // 线条name
positions: Cesium.Cartesian3.fromDegreesArray([
110.82474250408677, 22.095133670052277, 110.7480626299661,
22.261761037919744,
]), // 线条坐标***
material: new Cesium.PolylineDashMaterialProperty({
color: Cesium.Color.ORANGE, // 短划线颜色
dashLength: 20, // 短划线长度
}), // 线条材质:通过材质控制线条类型***
ddc: {
start: 0,
end: 150000000,
}, //显示在距相机的距离处的属性,多少区间内是可以显示的,default:{ start: 0,end: 150000000,}
show: true, // 是否显示线条,default:true
});
}
(2)仅必传参数代码
const initModel = () => {
me.line.entitiesLine({
id: "groundDashedLine", // 线条id***
positions: Cesium.Cartesian3.fromDegreesArray([
110.82474250408677, 22.095133670052277, 110.7480626299661,
22.261761037919744,
]), // 线条坐标***
material: new Cesium.PolylineDashMaterialProperty({
color: Cesium.Color.ORANGE, // 短划线颜色
dashLength: 20, // 短划线长度
}), // 线条材质:通过材质控制线条类型***
});
}
(3)效果
5、箭头线(2D)
(1)全参数代码
const initModel = () => {
me.line.entitiesLine({
id: "arrowLine", // 线条id***
name: "arrowLine", // 线条name
positions: Cesium.Cartesian3.fromDegreesArray([
110.82474250408677, 22.095133670052277, 110.7980626299661,
22.261761037919744,
]), // 线条坐标***
width:10,
material: new Cesium.PolylineArrowMaterialProperty(Cesium.Color.CYAN), // 线条材质:通过材质控制线条类型***
ddc: {
start: 0,
end: 150000000,
}, //显示在距相机的距离处的属性,多少区间内是可以显示的,default:{ start: 0,end: 150000000,}
show: true, // 是否显示线条,default:true
});
}
(2)仅必传参数代码
const initModel = () => {
me.line.entitiesLine({
id: "arrowLine", // 线条id***
positions: Cesium.Cartesian3.fromDegreesArray([
110.82474250408677, 22.095133670052277, 110.7980626299661,
22.261761037919744,
]), // 线条坐标***
material: new Cesium.PolylineArrowMaterialProperty(Cesium.Color.CYAN), // 线条材质:通过材质控制线条类型***
});
}
(3)效果
6、测向线(3D)
(1)全参数代码
const aggregate = ref([
110.82474250408677,
22.095133670052277,
0,
110.645025,
22.03158,
5000
])
const initModel = () => {
me.line.entitiesLine({
id: "sideLine", // 线条id***
name: "sideLine", // 线条name
positions: new Cesium.CallbackProperty(() => {
return aggregate.value;
}, false), // 线条坐标***
material: new Cesium.StripeMaterialProperty({
evenColor: Cesium.Color.WHITE,
oddColor: Cesium.Color.TOMATO,
repeat: 3,
}), // 线条材质***
width: 5, // 线条宽度
clampToGround: false, // 是否贴地
zIndex: 999, // 线条层级,clampToGround = true才生效
ddc: {
start: 0,
end: 150000000,
},
show: true,
});
}
// 更新测向线
updateSideLine(lon,lat,height){
aggregate.value = Cesium.Cartesian3.fromDegreesArrayHeights([
110.82474250408677,
22.095133670052277,
0,
lon, // 结束点经度
lat, // 结束点纬度
height, // 结束点高度
]);
}
(2)仅必传参数代码
const aggregate = ref([
110.82474250408677,
22.095133670052277,
0,
110.645025,
22.03158,
5000
])
const initModel = () => {
me.line.entitiesLine({
id: "sideLine", // 线条id***
positions: new Cesium.CallbackProperty(() => {
return aggregate.value;
}, false), // 线条坐标***
material: new Cesium.StripeMaterialProperty({
evenColor: Cesium.Color.WHITE,
oddColor: Cesium.Color.TOMATO,
repeat: 3,
}), // 线条材质***
});
}
// 更新测向线
updateSideLine(lon,lat,height){
aggregate.value = Cesium.Cartesian3.fromDegreesArrayHeights([
110.82474250408677,
22.095133670052277,
0,
lon, // 结束点经度
lat, // 结束点纬度
height, // 结束点高度
]);
}
(3)效果
四、primitivesLine示例
1、直线(2D)
(1)全参数代码
const initModel = () => {
me.line.primitivesLine({
// 参数依次为[经度1, 纬度1, 高度1, 经度2, 纬度2, 高度2]
positions: Cesium.PolylinePipeline.generateCartesianArc({
positions: Cesium.Cartesian3.fromDegreesArray([
110.82474250408677, 22.095133670052277, 110.8937798546077,
22.261761037919744,
]),
}), // 线条坐标***
material: Cesium.Material.fromType("Color", {
color: new Cesium.Color(5.0, 0, 0, 1.0),
}), // 线条材质:通过材质控制线条类型***
width: 2,
releaseGeometry: false,
geometryInstances: new Cesium.GeometryInstance({
id: "fluorescence", // 线条id***
name: "fluorescence", // 线条name
}),
ddc: {
start: 0,
end: 150000000,
}, //显示在距相机的距离处的属性,多少区间内是可以显示的
show: true, // 是否显示线条
});
}
(2)仅必传参数代码
const initModel = () => {
me.line.primitivesLine({
// 参数依次为[经度1, 纬度1, 高度1, 经度2, 纬度2, 高度2]
positions: Cesium.PolylinePipeline.generateCartesianArc({
positions: Cesium.Cartesian3.fromDegreesArray([
110.82474250408677, 22.095133670052277, 110.8937798546077,
22.261761037919744,
]),
}), // 线条坐标***
material: Cesium.Material.fromType("Color", {
color: new Cesium.Color(5.0, 0, 0, 1.0),
}), // 线条材质:通过材质控制线条类型***
});
}
(3)效果
2、Z字形线(2D)
(1)全参数代码
const initModel = () => {
me.line.primitivesLine({
// 参数依次为[经度1, 纬度1, 高度1, 经度2, 纬度2, 高度2]
positions: Cesium.PolylinePipeline.generateCartesianArc({
positions: Cesium.Cartesian3.fromDegreesArray([
110.82474250408677, 22.095133670052277, 110.99674745330135,
22.16643590658971, 110.82474250408677, 22.055133670052277,
110.99674745330135, 22.095133670052277,
]),
}), // 线条位置***
material: Cesium.Material.fromType(Cesium.Material.PolylineOutlineType, {
color: Cesium.Color.YELLOW,
outlineWidth: 3.0,
outlineColor: Cesium.Color.RED,
}), // 线条材质:通过材质控制线条类型***
width: 5,
releaseGeometry: false,
geometryInstances: new Cesium.GeometryInstance({
id: "fluorescence", // 线条id***
name: "fluorescence", // 线条name
}),
ddc: {
start: 0,
end: 150000000,
}, //显示在距相机的距离处的属性,多少区间内是可以显示的
show: true, // 是否显示线条
});
}
(2)仅必传参数代码
const initModel = () => {
me.line.primitivesLine({
// 参数依次为[经度1, 纬度1, 高度1, 经度2, 纬度2, 高度2]
positions: Cesium.PolylinePipeline.generateCartesianArc({
positions: Cesium.Cartesian3.fromDegreesArray([
110.82474250408677, 22.095133670052277, 110.99674745330135,
22.16643590658971, 110.82474250408677, 22.055133670052277,
110.99674745330135, 22.095133670052277,
]),
}), // 线条位置***
material: Cesium.Material.fromType(Cesium.Material.PolylineOutlineType, {
color: Cesium.Color.YELLOW,
outlineWidth: 3.0,
outlineColor: Cesium.Color.RED,
}), // 线条材质:通过材质控制线条类型***
});
}
(3)效果
3、荧光线(2D)
(1)全参数代码
const initModel = () => {
me.line.primitivesLine({
// 参数依次为[经度1, 纬度1, 高度1, 经度2, 纬度2, 高度2]
positions: Cesium.PolylinePipeline.generateCartesianArc({
positions: Cesium.Cartesian3.fromDegreesArray([
110.82474250408677, 22.095133670052277, 110.99674745330135, 22.03158,
]),
}), // 线条位置***
material: Cesium.Material.fromType(Cesium.Material.PolylineGlowType, {
glowPower: 0.2,
taperPower: 0.2,
color: new Cesium.Color(1.0, 0.5, 0.8, 1.0),
}), // 线条材质:通过材质控制线条类型***
width: 5,
releaseGeometry: false,
geometryInstances: new Cesium.GeometryInstance({
id: "fluorescence", // 线条id***
name: "fluorescence", // 线条name
}),
ddc: {
start: 0,
end: 150000000,
}, //显示在距相机的距离处的属性,多少区间内是可以显示的
show: true, // 是否显示线条
});
}
(2)仅必传参数代码
const initModel = () => {
me.line.primitivesLine({
// 参数依次为[经度1, 纬度1, 高度1, 经度2, 纬度2, 高度2]
positions: Cesium.PolylinePipeline.generateCartesianArc({
positions: Cesium.Cartesian3.fromDegreesArray([
110.82474250408677, 22.095133670052277, 110.99674745330135, 22.03158,
]),
}), // 线条位置***
material: Cesium.Material.fromType(Cesium.Material.PolylineGlowType, {
glowPower: 0.2,
taperPower: 0.2,
color: new Cesium.Color(1.0, 0.5, 0.8, 1.0),
}), // 线条材质:通过材质控制线条类型***
});
}
(3)效果
4、箭头线(2D)
(1)全参数代码
const initModel = () => {
me.line.primitivesLine({
// 参数依次为[经度1, 纬度1, 高度1, 经度2, 纬度2, 高度2]
positions: Cesium.PolylinePipeline.generateCartesianArc({
positions: Cesium.Cartesian3.fromDegreesArray([
110.82474250408677, 22.095133670052277, 110.8937798546077,
21.936190428417305,
]),
}), // 线条位置***
material: Cesium.Material.fromType(Cesium.Material.PolylineArrowType, {
color: Cesium.Color.CYAN,
}), // 线条材质:通过材质控制线条类型***
width: 5,
releaseGeometry: false,
geometryInstances: new Cesium.GeometryInstance({
id: "fluorescence", // 线条id***
name: "fluorescence", // 线条name
}),
ddc: {
start: 0,
end: 150000000,
}, //显示在距相机的距离处的属性,多少区间内是可以显示的
show: true, // 是否显示线条
});
}
(2)仅必传参数代码
const initModel = () => {
me.line.primitivesLine({
// 参数依次为[经度1, 纬度1, 高度1, 经度2, 纬度2, 高度2]
positions: Cesium.PolylinePipeline.generateCartesianArc({
positions: Cesium.Cartesian3.fromDegreesArray([
110.82474250408677, 22.095133670052277, 110.8937798546077,
21.936190428417305,
]),
}), // 线条位置***
material: Cesium.Material.fromType(Cesium.Material.PolylineArrowType, {
color: Cesium.Color.CYAN,
}), // 线条材质:通过材质控制线条类型***
});
}
(3)效果
5、拖尾线
(1)全参数代码
const initModel = () => {
me.line.primitivesLine({
// 参数依次为[经度1, 纬度1, 高度1, 经度2, 纬度2, 高度2]
positions: Cesium.PolylinePipeline.generateCartesianArc({
positions: Cesium.Cartesian3.fromDegreesArray([
110.7483984431316, 21.936190428417305, 110.82474250408677,
22.095133670052277,
]),
}), // 线条位置***
material: Cesium.Material.fromType(Cesium.Material.FadeType, {
repeat: false,
fadeInColor: Cesium.Color.CYAN,
fadeOutColor: Cesium.Color.CYAN.withAlpha(0),
time: new Cesium.Cartesian2(0.0, 0.0),
fadeDirection: {
x: true,
y: false,
},
}), // 线条材质:通过材质控制线条类型***
width: 5,
releaseGeometry: false,
geometryInstances: new Cesium.GeometryInstance({
id: "fluorescence", // 线条id***
name: "fluorescence", // 线条name
}),
ddc: {
start: 0,
end: 150000000,
}, //显示在距相机的距离处的属性,多少区间内是可以显示的
show: true, // 是否显示线条
});
}
(2)仅必传参数代码
const initModel = () => {
me.line.primitivesLine({
// 参数依次为[经度1, 纬度1, 高度1, 经度2, 纬度2, 高度2]
positions: Cesium.PolylinePipeline.generateCartesianArc({
positions: Cesium.Cartesian3.fromDegreesArray([
110.7483984431316, 21.936190428417305, 110.82474250408677,
22.095133670052277,
]),
}), // 线条位置***
material: Cesium.Material.fromType(Cesium.Material.FadeType, {
repeat: false,
fadeInColor: Cesium.Color.CYAN,
fadeOutColor: Cesium.Color.CYAN.withAlpha(0),
time: new Cesium.Cartesian2(0.0, 0.0),
fadeDirection: {
x: true,
y: false,
},
}), // 线条材质:通过材质控制线条类型***
});
}