Cesium 测量

一、简介

绘制polyline、polygon,然后计算距离和面积,然后显示标签

双击结束绘制,右键结束绘制。

二、效果

 

 

 

 

 

三、部分代码

复制代码

/*
 * @Author: 苹果园dog
 * @Date: 2021-01-09 16:17:38
 * @LastEditTime: 2021-01-09 19:55:57
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \code\bayannaoer.web.vue\src\commonUtil\CesiumMeasure.js
 */
import cesiumMeasureUtil from '../utils/commonUtil/cesiumMeasureUtil';

/**
 * 绘制折线
 * @param {*} viewer
 * @param {*} options
 */

function DrawPolyLine(viewer, options) {
    this.viewer = viewer;
    this.ismeare = options.ismeare || false;
    this.positions = [];
    this.poly = null;
    this.tooltip = document.getElementById(options.toolTip || "toolTip");
    this.cartesian = null;
    this.floatingPoint = null;
    this.mouseHandler = null;
    this.onCompleted = options.onCompleted;
    this._pointLabels = [];
    this._totalLengthPointLabel = undefined;
}



/**
 * 清除绘制
 */
DrawPolyLine.prototype.clear = function () {
    let removeLayerName = ["drawpolyline", "drawpolylinepoint"];
    for (var i = 0; i < window.viewer.entities.values.length; i++) {
        var entity = window.viewer.entities.values[i];
        if (removeLayerName.indexOf(entity.name) > -1) {
            window.viewer.entities.removeById(entity.id);
            i--;
        }
    }

    this.positions = [];
    this.poly = null;
    this.cartesian = null;
    this.floatingPoint = null;
    if (this.tooltip) {
        this.tooltip.style.display = "none";
    }
    if (this.mouseHandler) {
        this.mouseHandler.destroy();
        this.mouseHandler = null;
    }
    if (this.floatingPoint) {
        this.viewer.entities.remove(this.floatingPoint);
    }
    this._pointLabels = [];
    this._totalLengthPointLabel = undefined;
};

/**
 * 开始绘制
 */
DrawPolyLine.prototype.startDraw = function () {
    this.clear();
    let tooltip = this.tooltip;
    this.ismeare = true;
    let ismeare = this.ismeare;
    let viewer = this.viewer;
    let cartesian = this.cartesian;
    let positions = this.positions;
    let floatingPoint = this.floatingPoint;
    this.mouseHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    let handler = this.mouseHandler;
    let poly = this.poly;
    let that = this;
    //鼠标移动事件
    handler.setInputAction(function (movement) {
        if (!ismeare) {
            return;
        }
        tooltip.style.display = "block";
        tooltip.style.left = movement.endPosition.x + 3 + "px";
        tooltip.style.top = movement.endPosition.y - 25 + "px";
        tooltip.innerHTML = "<p>单击开始,双击结束,右键取消</p>";
        let ray = viewer.camera.getPickRay(movement.endPosition);
        cartesian = viewer.scene.globe.pick(ray, viewer.scene);
        if (positions.length >= 2) {
            if (!Cesium.defined(poly)) {
                poly = new PolyLinePrimitive(positions);
            } else {
                positions.pop();
                positions.push(cartesian);
            }
            that.showLengthLabel();
        }
        viewer.scene.postProcessStages.fxaa.enabled = false; //去锯齿
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

    //鼠标左键单击事件
    handler.setInputAction(function (movement) {
        if (!ismeare) {
            return;
        }
        let ray = viewer.camera.getPickRay(movement.position);
        cartesian = viewer.scene.globe.pick(ray, viewer.scene);
        if (!cartesian) {
            return;
        }
        if (ismeare) {
            tooltip.style.display = "none";
            if (!cartesian) {
                return;
            }
            if (positions.length == 0) {
                positions.push(cartesian.clone());
            }
            positions.push(cartesian);
            floatingPoint = viewer.entities.add({
                name: "drawpolylinepoint",
                position: positions[positions.length - 1],
                point: {
                    pixelSize: 4,
                    color: Cesium.Color.RED,
                    outlineColor: Cesium.Color.WHITE,
                    outlineWidth: 2,
                },
            });
        }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

    /**
     * 双击事件
     */
    handler.setInputAction(function (movement) {
        if (!ismeare) {
            return;
        }
        that.ismeare = false;
        that.positions.pop();
        if (floatingPoint) {
            that.viewer.entities.remove(floatingPoint);
        }
        that.tooltip.style.display = "none";
        that.viewer.selectedEntity = null;
        that.viewer.trackedEntity = null;
        if (that.onCompleted) {
            that.onCompleted(that.positions);
        }
        if (that.mouseHandler) {
            that.mouseHandler.destroy();
            that.mouseHandler = null;
        }
    }, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);



    /**
     * 右键单击事件取消绘制操作
     */
    handler.setInputAction(function (movement) {
        that.clear();
    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);

复制代码

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苹果园dog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值