Source: modules/measure/MeasureMultiDistance.js

import * as Cesium from "cesium";

/**
 * MeasureMultiDistance class for measuring multi distances in a Cesium viewer.
 * @class MeasureMultiDistance
 * @param {Object} viewer - The Cesium viewer instance.
 * @param {Object} [options] - Options for the measurement tool.
 * @param {Cesium.Color} [options.color] - The color of the measurement line and points.
 * @param {boolean} [options.clampToGround] - Whether to clamp the measurement to the ground.
 * @example
 * const measureMultiDistance = new MeasureMultiDistance(viewer, { color: Cesium.Color.RED });
 * measureMultiDistance.on();
 * // To disable the measurement tool and clear entities:
 * measureMultiDistance.off();
 */
export class MeasureMultiDistance {
    constructor(viewer, options = {}) {
        this.viewer = viewer;
        this.scene = viewer.scene;
        this.handler = new Cesium.ScreenSpaceEventHandler();
        this.color = options.color || Cesium.Color.LIGHTGRAY;
        this.clampToGround = options.clampToGround || false;
        this.status = false;
        this.cartesians = undefined;
        this.endCartesian = undefined;
        this.polylineEntity = undefined;
        this.pointEntities = [];
    }

    /**
     * Enables the measurement tool.
     * Click to start measuring, and click again drawing the lines.
     * Left click to add points, right click to finish.
     * @function
     * @type {boolean} Continue - Whether to continue measuring after the first click.
     * @returns {void}
     */
    on = (isContinue = false) => {
        this.scene.canvas.style.cursor = "crosshair";
        const viewer = this.viewer;
        const scene = viewer.scene;
        const handler = this.handler;

        const mouseLeftClickHandler = (event) => {
            if (!this.status) {
                this.status = true;
                this.clearEntities(viewer);
                this.clearCartesians();
                scene.screenSpaceCameraController.enableRotate = true;
                scene.screenSpaceCameraController.enableTranslate = true;
                scene.screenSpaceCameraController.enableZoom = true;
                scene.screenSpaceCameraController.enableTilt = true;
                scene.screenSpaceCameraController.enableLook = true;
            }

            this.pickedObject = scene.pick(event.position);
            let pickedEllipsoidPosition;
            if (scene.pickPositionSupported) {
                pickedEllipsoidPosition = viewer.scene.pickPosition(
                    event.position);
            }
            if (!pickedEllipsoidPosition) {
                pickedEllipsoidPosition = viewer.camera.pickEllipsoid(
                    event.position,
                    scene.globe.ellipsoid,
                );
                const cartographic = Cesium.Cartographic.fromCartesian(
                    pickedEllipsoidPosition);
                const height = viewer.scene.globe.getHeight(
                    Cesium.Cartographic.fromRadians(cartographic.longitude,
                        cartographic.latitude, 0));
                pickedEllipsoidPosition = Cesium.Cartesian3.fromRadians(
                    cartographic.longitude, cartographic.latitude, height);
            }

            // prevent adding the same point
            if (this.cartesians.length > 0) {
                const lastCartesian = this.cartesians[this.cartesians.length -
                1];
                console.log(lastCartesian, pickedEllipsoidPosition);
                if (Cesium.Cartesian3.equals(lastCartesian,
                    pickedEllipsoidPosition)) {
                    console.log("same point");
                    return;
                }
            }

            this.cartesians.push(pickedEllipsoidPosition);
            this.endCartesian = pickedEllipsoidPosition;

            const pointEntity = viewer.entities.add({
                position: pickedEllipsoidPosition,
                point: {
                    show: false,
                    color: this.color,
                    pixelSize: 4,
                    disableDepthTestDistance: Number.POSITIVE_INFINITY,
                },
            });

            if (this.pointEntities.length > 0) {
                const lastCartesian = this.cartesians[this.cartesians.length -
                2];
                const list = [lastCartesian, pickedEllipsoidPosition];
                const distance = this.calculateDistance(list);

                const center = Cesium.Cartesian3.add(lastCartesian,
                    pickedEllipsoidPosition, new Cesium.Cartesian3());
                Cesium.Cartesian3.multiplyByScalar(center, 0.5, center);

                const labelEntity = viewer.entities.add({
                    position: center,
                    label: {
                        show: true,
                        showBackground: false,
                        font: "14px monospace",
                        fillColor: this.color,
                        horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                        verticalOrigin: Cesium.VerticalOrigin.TOP,
                        pixelOffset: new Cesium.Cartesian2(0, -20),
                        disableDepthTestDistance: Number.POSITIVE_INFINITY,
                        text: `${distance}`,
                    },
                });
                this.pointEntities.push(labelEntity);
            }

            this.pointEntities.push(pointEntity);

            if (this.cartesians.length <= 1) {
                this.polylineEntity = viewer.entities.add({
                    polyline: {
                        positions: new Cesium.CallbackProperty(() => {
                            const cartesianPositions = this.cartesians.slice();
                            cartesianPositions.push(this.endCartesian);
                            // cartesianPositions.push(this.cartesians[0]);
                            return cartesianPositions;
                        }, false),
                        width: 3,
                        material: this.color.withAlpha(0.8),
                        depthFailMaterial: this.color,
                        clampToGround: this.clampToGround,
                    },
                });
            }
        };

        const mouseMoveHandler = async (moveEvent) => {
            if (!this.status) {
                return;
            }
            // pickedObject = scene.pick(moveEvent.endPosition);
            let pickedEllipsoidPosition;
            if (scene.pickPositionSupported) {
                pickedEllipsoidPosition = viewer.scene.pickPosition(
                    moveEvent.endPosition);
            }
            if (!pickedEllipsoidPosition) {
                pickedEllipsoidPosition = viewer.camera.pickEllipsoid(
                    moveEvent.endPosition,
                    scene.globe.ellipsoid,
                );
                const cartographic = Cesium.Cartographic.fromCartesian(
                    pickedEllipsoidPosition);
                const height = viewer.scene.globe.getHeight(
                    Cesium.Cartographic.fromRadians(cartographic.longitude,
                        cartographic.latitude, 0));
                pickedEllipsoidPosition = Cesium.Cartesian3.fromRadians(
                    cartographic.longitude, cartographic.latitude, height);
            }
            this.endCartesian = pickedEllipsoidPosition;
        };

        const mouseRightClickHandler = (event) => {
            this.status = false;

            this.pickedObject = scene.pick(event.position);
            let pickedEllipsoidPosition;
            if (scene.pickPositionSupported) {
                pickedEllipsoidPosition = viewer.scene.pickPosition(
                    event.position);
            }
            if (!pickedEllipsoidPosition) {
                pickedEllipsoidPosition = viewer.camera.pickEllipsoid(
                    event.position,
                    scene.globe.ellipsoid,
                );
                const cartographic = Cesium.Cartographic.fromCartesian(
                    pickedEllipsoidPosition);
                const height = viewer.scene.globe.getHeight(
                    Cesium.Cartographic.fromRadians(cartographic.longitude,
                        cartographic.latitude, 0));
                pickedEllipsoidPosition = Cesium.Cartesian3.fromRadians(
                    cartographic.longitude, cartographic.latitude, height);
            }

            this.cartesians.push(pickedEllipsoidPosition);

            const distance = this.calculateDistance(this.cartesians);
            const pointEntity = viewer.entities.add({
                position: pickedEllipsoidPosition,
                point: {
                    show: false,
                    color: this.color,
                    pixelSize: 4,
                    disableDepthTestDistance: Number.POSITIVE_INFINITY,
                },
                label: {
                    // show: true,
                    showBackground: false,
                    font: "14px monospace",
                    fillColor: this.color,
                    horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                    verticalOrigin: Cesium.VerticalOrigin.TOP,
                    pixelOffset: new Cesium.Cartesian2(0, -20),
                    disableDepthTestDistance: Number.POSITIVE_INFINITY,
                    // outlineColor: Cesium.Color.DARKGRAY,
                    // outlineWidth: 2,
                    // style: Cesium.LabelStyle.FILL_AND_OUTLINE,
                    text: `(${distance})`,
                },
            });
            this.pointEntities.push(pointEntity);

            scene.screenSpaceCameraController.enableRotate = true;
            scene.screenSpaceCameraController.enableTranslate = true;
            scene.screenSpaceCameraController.enableZoom = true;
            scene.screenSpaceCameraController.enableTilt = true;
            scene.screenSpaceCameraController.enableLook = true;
        };
        handler.setInputAction(mouseLeftClickHandler,
            Cesium.ScreenSpaceEventType.LEFT_CLICK);
        handler.setInputAction(mouseMoveHandler,
            Cesium.ScreenSpaceEventType.MOUSE_MOVE);
        handler.setInputAction(mouseRightClickHandler,
            Cesium.ScreenSpaceEventType.RIGHT_CLICK);
        handler.setInputAction(mouseRightClickHandler,
            Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
    };

    /**
     * Disables the measurement tool and clears the entities.
     * @function
     * @returns {void}
     */
    off = () => {
        this.scene.canvas.style.cursor = "default";
        this.status = false;
        this.clearEntities();
        const handler = this.handler;
        if (handler) {
            handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
            handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
            handler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK);
            handler.removeInputAction(
                Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
        }
    };

    clearEntities = () => {
        this.viewer.entities.remove(this.polylineEntity);
        // this.viewer.entities.remove(this.polygonEntity);
        this.pointEntities.forEach(entity => {
            this.viewer.entities.remove(entity);
        });
        this.polylineEntity = undefined;
        // this.polygonEntity = undefined;
        this.pointEntities = [];
        this.cartesians = [];
        this.endCartesian = undefined;
    };

    clearCartesians = () => {
        this.cartesians = [];
        this.endCartesian = undefined;
    };

    calculateDistance = (cartesians) => {
        const positions = cartesians;
        let distance = 0;
        for (let i = 0; i < positions.length - 1; i++) {
            distance += Cesium.Cartesian3.distance(positions[i],
                positions[i + 1]);
        }
        // distance = Cesium.Math.toDegrees(distance);
        if (distance > 1000) {
            distance = `${(distance / 1000).toFixed(3)}km`;
        } else {
            distance = `${distance.toFixed(3)}m`;
        }
        return distance;
    };

}