Cesium 绘制经纬网,方格里不显示经纬度


前言

其实调用图层叠加是最好的方式,又省事。但是也记录一下前端自己生成的吧。


效果图如下:

1、具体代码

/**
 * 经纬网实现
 */
import {
  Color,
  Cartographic,
  Cartesian3,
  PolylineCollection,
  PolylineDashMaterialProperty
} from "cesium";
export class Graticules {
  constructor(viewer, options) {
    this.viewer = viewer;
    this.lines = this.viewer.scene.primitives.add(new PolylineCollection());
    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;

    // 移除 attribution 标志
    this.viewer._cesiumWidget._creditContainer.style.display = "none";
    // 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
        // material: PolylineDashMaterialProperty({
        //   color: Cesium.Color.WHITE.withAlpha(0.5)
        // })
        // material: this.color  // 加上材质报错
      });
    }
  }
  // 绘制纬线
  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
        // material: PolylineDashMaterialProperty({
        //   color: Cesium.Color.WHITE.withAlpha(0.5)
        // })
        // material: this.color
      });
    }
  }

  render() {
    console.log("执行");
    this.drawLongLines();
    this.drawLatLines();
    return this.lines;
  }

  clear() {
    this.lines.removeAll();
  }

  destroy() {
    this.lines.destroy();
  }
}

2、使用 

<script>
import { Graticules } from "@/utils/map/Graticules";
export default {
  data() {
    return {
    };
  },
  mounted() {
    if (window.viewer) {
       let newMap = new Graticules(window.viewer);
       newMap.render();
     }
  },
</script>

总结

发现同事调用的图层,代码更加简洁,但API明显是用的公司封装的,等问清楚了 再来补充

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值