此篇博客主要基于vanora1111博主绘制经纬网文章进行添加经纬度标注。
/**
* 经纬网实现
*/
import {
Color,
Cartographic,
Cartesian3,
PolylineCollection,
} from "cesium";
export class Graticules {
constructor(viewer, options) {
this.viewer = viewer;
this.lines = this.viewer.scene.primitives.add(new PolylineCollection());
this.entities = this.viewer.entities;
this.color = Color.fromCssColorString("#ffffff").withAlpha(0.5);
this.width = options?.width ?? 1;
this.viewer.scene.globe.enableLighting = false; // 禁用灯光照射
// 隐藏默认的经纬网格
this.viewer.scene.globe.showGroundAtmosphere = false;
this.viewer.scene.globe.showWaterEffect = false;
this.label = []
// 移除 attribution 标志
this.viewer._cesiumWidget._creditContainer.style.display = "none";
this.polylineLabelMap = new Map(); // 用于跟踪折线和标签的映射
// this.render();
}
// 绘制经线
drawLongLines() {
for (let lon = -180; lon <= 180; lon += 10) {
const positions = [];
for (let lat = -90; lat <= 90; lat += 1) {
const cartographic = Cartographic.fromDegrees(lon, lat);
const position =
this.viewer.scene.globe.ellipsoid.cartographicToCartesian(
cartographic
);
positions.push(position);
}
this.lines.add({
positions: positions,
width: this.width,
})
this.label.push(this.viewer.entities.add({
position : Cesium.Cartesian3.fromDegrees(lon, 360),
label : {
text : lon.toString()+'°',
font: '18px sans-serif',
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
}
}))
}
}
// 绘制纬线
drawLatLines() {
// 计算纬度线的位置
for (let lat = -90; lat <= 90; lat += 10) {
const positions = [];
for (let lon = -180; lon <= 180; lon += 1) {
const cartographic = Cartographic.fromDegrees(lon, lat);
const position =
this.viewer.scene.globe.ellipsoid.cartographicToCartesian(
cartographic
);
positions.push(position);
}
this.lines.add({
positions: positions,
width: this.width
});
this.label.push(this.viewer.entities.add({
position : Cesium.Cartesian3.fromDegrees(180,lat),
label : {
text : lat.toString()+'°',
font: '18px sans-serif',
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
clampToGround: true,
}
}))
}
}
render() {
this.drawLongLines();
this.drawLatLines();
return this.lines;
}
clear() {
this.lines.removeAll();
this.clearLabel()
}
destroy() {
this.lines.destroy();
this.clearLabel()
}
clearLabel() {
this.label.forEach((item) => {
this.viewer.entities.remove(item);
})
}
}
import { Graticules } from "@/utils/map/graticules";
let newMap
let graticulesFlag = ref(false)
if(!newMap){
newMap = new Graticules(earthViewer.basicMap3d.viewer);
}
graticulesFlag.value = !graticulesFlag.value
if (graticulesFlag.value) {
newMap.render();
}
else {
newMap.clear();
}
在此感谢!