Men的博客

欢迎光临!

0%

Cesium测量

import proj4 from “proj4”;

if (typeof Cesium !== ‘undefined’)
/**

 * @author zhangti
 * @param viewer  {object} 三维对象
 * @param options {object} 初始化参数
 * @constructor
 */
Cesium.Measure = (function (Cesium) {

    /**
     * 绘制对象
     * @param viewer
     * @param options
     * @constructor
     */
    function _(viewer, options = {}) {

        if (viewer && viewer instanceof Cesium.Viewer) {

            this._drawLayer = new Cesium.CustomDataSource('measureLayer');

            viewer && viewer.dataSources.add(this._drawLayer);

            this._basePath = options.basePath || '';

            this._viewer = viewer;
            
            this.handler = null
        }
    }

    _.prototype = {
        /***
         * 坐标转换 84转笛卡尔
         *
         * @param {Object} {lng,lat,alt} 地理坐标
         *
         * @return {Object} Cartesian3 三维位置坐标
         */
        transformWGS84ToCartesian: function (position, alt) {
            if (this._viewer) {
                return position
                    ? Cesium.Cartesian3.fromDegrees(
                        position.lng || position.lon,
                        position.lat,
                        position.alt = alt || position.alt,
                        Cesium.Ellipsoid.WGS84
                    )
                    : Cesium.Cartesian3.ZERO
            }
        },
        /***
         * 坐标数组转换 笛卡尔转84
         *
         * @param {Array} WSG84Arr {lng,lat,alt} 地理坐标数组
         * @param {Number} alt 拔高
         * @return {Array} Cartesian3 三维位置坐标数组
         */
        transformWGS84ArrayToCartesianArray: function (WSG84Arr, alt) {
            if (this._viewer && WSG84Arr) {
                var $this = this
                return WSG84Arr
                    ? WSG84Arr.map(function (item) {
                        return $this.transformWGS84ToCartesian(item, alt)
                    })
                    : []
            }
        },
        /***
         * 坐标转换 笛卡尔转84
         *
         * @param {Object} Cartesian3 三维位置坐标
         *
         * @return {Object} {lng,lat,alt} 地理坐标
         */
        transformCartesianToWGS84: function (cartesian) {
            if (this._viewer && cartesian) {
                var ellipsoid = Cesium.Ellipsoid.WGS84
                var cartographic = ellipsoid.cartesianToCartographic(cartesian)
                return {
                    lng: Cesium.Math.toDegrees(cartographic.longitude),
                    lat: Cesium.Math.toDegrees(cartographic.latitude),
                    alt: cartographic.height
                }
            }
        },
        /***
         * 坐标数组转换 笛卡尔转86
         *
         * @param {Array} cartesianArr 三维位置坐标数组
         *
         * @return {Array} {lng,lat,alt} 地理坐标数组
         */
        transformCartesianArrayToWGS84Array: function (cartesianArr) {
            if (this._viewer) {
                var $this = this
                return cartesianArr
                    ? cartesianArr.map(function (item) {
                        return $this.transformCartesianToWGS84(item)
                    })
                    : []
            }
        },
        /**
         * 84坐标转弧度坐标
         * @param {Object} position wgs84
         * @return {Object} Cartographic 弧度坐标
         *
         */
        transformWGS84ToCartographic: function (position) {
            return position
                ? Cesium.Cartographic.fromDegrees(
                    position.lng || position.lon,
                    position.lat,
                    position.alt
                )
                : Cesium.Cartographic.ZERO
        },
        /**
         * 拾取位置点
         *
         * @param {Object} px 屏幕坐标
         *
         * @return {Object} Cartesian3 三维坐标
         */
        getCatesian3FromPX: function (px) {

            if (this._viewer && px) {
                var picks = this._viewer.scene.drillPick(px)
                var cartesian = null;
                var isOn3dtiles = false, isOnTerrain = false;
                // drillPick
                for (let i in picks) {
                    let pick = picks[i]

                    if (pick &&
                        pick.primitive instanceof Cesium.Cesium3DTileFeature
                        || pick && pick.primitive instanceof Cesium.Cesium3DTileset
                        || pick && pick.primitive instanceof Cesium.Model) { //模型上拾取
                        isOn3dtiles = true;
                    }
                    // 3dtilset
                    if (isOn3dtiles) {
                        this._viewer.scene.pick(px) // pick
                        cartesian = this._viewer.scene.pickPosition(px);
                        if (cartesian) {
                            let cartographic = Cesium.Cartographic.fromCartesian(cartesian);
                            if (cartographic.height < 0) cartographic.height = 0;
                            let lon = Cesium.Math.toDegrees(cartographic.longitude)
                                , lat = Cesium.Math.toDegrees(cartographic.latitude)
                                , height = cartographic.height;
                            cartesian = this.transformWGS84ToCartesian({lng: lon, lat: lat, alt: height})

                        }
                    }
                }
                // 地形
                let boolTerrain = this._viewer.terrainProvider instanceof Cesium.EllipsoidTerrainProvider;
                // Terrain
                if (!isOn3dtiles && !boolTerrain) {
                    var ray = this._viewer.scene.camera.getPickRay(px);
                    if (!ray) return null;
                    cartesian = this._viewer.scene.globe.pick(ray, this._viewer.scene);
                    isOnTerrain = true
                }
                // 地球
                if (!isOn3dtiles && !isOnTerrain && boolTerrain) {

                    cartesian = this._viewer.scene.camera.pickEllipsoid(px, this._viewer.scene.globe.ellipsoid);
                }
                if (cartesian) {
                    let position = this.transformCartesianToWGS84(cartesian)
                    if (position.alt < 0) {
                        cartesian = this.transformWGS84ToCartesian(position, 0.1)
                    }
                    return cartesian;
                }
                return false;
            }

        },
        /**
         * 获取84坐标的距离
         * @param {*} positions
         */
        getPositionDistance: function (positions) {
            let distance = 0
            for (let i = 0; i < positions.length - 1; i++) {
                let point1cartographic = this.transformWGS84ToCartographic(positions[i])
                let point2cartographic = this.transformWGS84ToCartographic(positions[i + 1])
                let geodesic = new Cesium.EllipsoidGeodesic()
                geodesic.setEndPoints(point1cartographic, point2cartographic)
                let s = geodesic.surfaceDistance
                s = Math.sqrt(
                    Math.pow(s, 2) +
                    Math.pow(point2cartographic.height - point1cartographic.height, 2)
                )
                distance = distance + s
            }
            return distance.toFixed(3)
        },
        /**
         * 计算一组坐标组成多边形的面积
         * @param {*} positions
         */
        getPositionsArea: function (positions) {
            let result = 0
            if (positions) {
                let h = 0
                let ellipsoid = Cesium.Ellipsoid.WGS84
                positions.push(positions[0])
                for (let i = 1; i < positions.length; i++) {
                    let oel = ellipsoid.cartographicToCartesian(
                        this.transformWGS84ToCartographic(positions[i - 1])
                    )
                    let el = ellipsoid.cartographicToCartesian(
                        this.transformWGS84ToCartographic(positions[i])
                    )
                    h += oel.x * el.y - el.x * oel.y
                }
                result = Math.abs(h).toFixed(2)
            }
            return result
        },
        /**
         * 坐标查看
         */
        coordinateCheck: function (options = {}) {
            if (this._viewer && options) {

                var positions = [], $this = this;
                $this.handler = new Cesium.ScreenSpaceEventHandler(this._viewer.scene.canvas);
                // left
                $this.handler.setInputAction(function (movement) {

                    var cartesian = $this.getCatesian3FromPX(movement.position);
                    if (cartesian && cartesian.x) {
                        //if (positions.length === 0) {
                        //    positions = cartesian.clone();
                        // }
                        // 添加量测信息点
                        // console.log(cartesian);
                        _addInfoPoint(cartesian)
                        //positions.push(cartesian);
                    }
                }, Cesium.ScreenSpaceEventType.LEFT_CLICK);


                // right
                $this.handler.setInputAction(function (movement) {
                    $this.handler.destroy();
                }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);




                //添加坐标点
                function _addInfoPoint(position) {
                    let wgs84Position = $this.transformCartesianToWGS84(position)
                    var wgs84 = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs  ';
                    var WHCGCE2000 = '+proj=tmerc +lat_0=0 +lon_0=114.3333333333333 +k=1 +x_0=800000 +y_0=-3000000.0 +ellps=GRS80 +units=m +no_defs';
                    let newposition = proj4(wgs84, WHCGCE2000, [wgs84Position.lng, wgs84Position.lat,wgs84Position.alt]);
                    options.cb&&options.cb(newposition)
                    var _labelEntity = new Cesium.Entity()
                    _labelEntity.position = position
                    _labelEntity.point = {
                        pixelSize: 8,
                        outlineColor: Cesium.Color.LAWNGREEN,
                        outlineWidth: 2
                    }
                    _labelEntity.label = {
                        text: `${newposition[0]},${newposition[1]}`,//($this.getPositionDistance($this.transformCartesianArrayToWGS84Array(positions)) / 1000).toFixed(4) + '公里',
                        show: true,
                        showBackground: true,
                        font: '12px Helvetica',
                        horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
                        verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                        pixelOffset: new Cesium.Cartesian2(-20, -30) //left top
                    }
                    $this._drawLayer.entities.add(_labelEntity)
                }
            }
        },
        /**
         * 
         * @param {*} callback 
         */
        returnCoordinate:function(callback){
            if (this._viewer && callback) {
                var $this = this;
                var heading,pitch;
                $this.handler = new Cesium.ScreenSpaceEventHandler(this._viewer.scene.canvas);
                // left
                $this.handler.setInputAction(function (movement) {
              
                    var cartesian = $this.getCatesian3FromPX(movement.position);
                    let wgs84Position = $this.transformCartesianToWGS84(cartesian);
                    var camera =  $this._viewer.scene.camera;
                    var cartesian2 = $this._viewer.scene.pickPosition(movement.position);
                    if (cartesian2) {
                        heading = Cesium.Math.toDegrees(camera.heading);
                        pitch = Cesium.Math.toDegrees(camera.pitch);
                    }
                    callback({...wgs84Position,heading,pitch});
                }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
            }
        },
        /**
         * 测距
         * @param {*} options
         */
        drawLineMeasureGraphics: function (options = {}) {
           

            if (this._viewer && options) {

                console.log('测距2',this)

                var positions = [], _lineEntity = new Cesium.Entity(), $this = this, lineObj;

                $this.handler = new Cesium.ScreenSpaceEventHandler($this._viewer.scene.canvas);
                // left
                $this.handler.setInputAction(function (movement) {

                    var cartesian = $this.getCatesian3FromPX(movement.position);
                    if (cartesian && cartesian.x) {
                        if (positions.length == 0) {
                            positions.push(cartesian.clone());
                        }
                        // 添加量测信息点
                        _addInfoPoint(cartesian)
                        positions.push(cartesian);
                    }
                }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

                $this.handler.setInputAction(function (movement) {

                    var cartesian = $this.getCatesian3FromPX(movement.endPosition);
                    if (positions.length >= 2) {
                        if (cartesian && cartesian.x) {
                            positions.pop();
                            positions.push(cartesian);
                        }
                    }
                }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
                // right
                $this.handler.setInputAction(function (movement) {

                    $this.handler.destroy()
                    $this.handler = null

                    let cartesian = $this.getCatesian3FromPX(movement.position);
                    _addInfoPoint(cartesian)

                    if (typeof options.callback === 'function') {
                        options.callback($this.transformCartesianArrayToWGS84Array(positions), lineObj);
                    }
                }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);

                _lineEntity.polyline = {
                    width: options.width || 5
                    , material: options.material || Cesium.Color.BLUE.withAlpha(0.8)
                    , clampToGround: options.clampToGround || false
                }
                _lineEntity.polyline.positions = new Cesium.CallbackProperty(function () {
                    return positions
                }, false)
                lineObj = this._drawLayer.entities.add(_lineEntity)

                //添加坐标点
                function _addInfoPoint(position) {
                    var _labelEntity = new Cesium.Entity()
                    _labelEntity.position = position
                    _labelEntity.point = {
                        pixelSize: 10,
                        outlineColor: Cesium.Color.BLUE,
                        outlineWidth: 5
                    }
                    _labelEntity.label = {
                        text: ($this.getPositionDistance($this.transformCartesianArrayToWGS84Array(positions)) / 1000).toFixed(4) + '公里',
                        show: true,
                        showBackground: true,
                        font: '14px monospace',
                        horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
                        verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                        pixelOffset: new Cesium.Cartesian2(-20, -80) //left top
                    }
                    $this._drawLayer.entities.add(_labelEntity)
                }
            }

        },
        /**
         * 测面积
         * @param {*} options
         */
        drawAreaMeasureGraphics: function (options = {}) {

            if (this._viewer && options) {

                var positions = [], polygon = new Cesium.PolygonHierarchy(), _polygonEntity = new Cesium.Entity(),
                    $this = this, polyObj = null, _label = '';
                $this.handler = new Cesium.ScreenSpaceEventHandler(this._viewer.scene.canvas);
                // left
                $this.handler.setInputAction(function (movement) {

                    var cartesian = $this.getCatesian3FromPX(movement.position);
                    if (cartesian && cartesian.x) {
                        if (positions.length == 0) {
                            polygon.positions.push(cartesian.clone())
                            positions.push(cartesian.clone());
                        }
                        positions.push(cartesian.clone());
                        polygon.positions.push(cartesian.clone())

                        if (!polyObj) create()
                    }
                }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
                // mouse
                $this.handler.setInputAction(function (movement) {

                    var cartesian = $this.getCatesian3FromPX(movement.endPosition);
                    // var cartesian = $this._viewer.scene.camera.pickEllipsoid(movement.endPosition, $this._viewer.scene.globe.ellipsoid);
                    if (positions.length >= 2) {
                        if (cartesian && cartesian.x) {
                            positions.pop()
                            positions.push(cartesian);
                            polygon.positions.pop()
                            polygon.positions.push(cartesian);
                        }
                    }
                }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

                // right
                $this.handler.setInputAction(function (movement) {
                    let cartesian = $this.getCatesian3FromPX(movement.endPosition);

                    $this.handler.destroy();

                    positions.push(positions[0]);

                    // 添加信息点
                    _addInfoPoint(positions[0])
                    if (typeof options.callback === 'function') {

                        options.callback($this.transformCartesianArrayToWGS84Array(positions), polyObj);
                    }
                }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);

                function create() {
                    _polygonEntity.polyline = {
                        width: 3
                        , material: Cesium.Color.BLUE.withAlpha(0.8)
                        , clampToGround: options.clampToGround || false
                    }

                    _polygonEntity.polyline.positions = new Cesium.CallbackProperty(function () {
                        return positions
                    }, false)

                    _polygonEntity.polygon = {

                        hierarchy: new Cesium.CallbackProperty(function () {
                            return polygon
                        }, false),

                        material: Cesium.Color.WHITE.withAlpha(0.1)
                        , clampToGround: options.clampToGround || false
                    }
                    polyObj = $this._drawLayer.entities.add(_polygonEntity)
                }

                function _addInfoPoint(position) {
                    var _labelEntity = new Cesium.Entity()
                    _labelEntity.position = position
                    _labelEntity.point = {
                        pixelSize: 10,
                        outlineColor: Cesium.Color.BLUE,
                        outlineWidth: 5
                    }
                    _labelEntity.label = {
                        text: ($this.getPositionsArea($this.transformCartesianArrayToWGS84Array(positions)) / 1000000.0).toFixed(4) + '平方公里',
                        show: true,
                        showBackground: true,
                        font: '14px monospace',
                        horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
                        verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                        pixelOffset: new Cesium.Cartesian2(-20, -50) //left top
                    }
                    $this._drawLayer.entities.add(_labelEntity)
                }
            }

        },
        /**
         * 画三角量测
         * @param {*} options
         */
        drawTrianglesMeasureGraphics: function (options = {}) {
            options.style = options.style ||
                {
                    width: 3
                    , material: Cesium.Color.BLUE.withAlpha(0.5)
                }
            if (this._viewer && options) {

                var _trianglesEntity = new Cesium.Entity(), _tempLineEntity = new Cesium.Entity(),
                    _tempLineEntity2 = new Cesium.Entity(),
                    _positions = [], _tempPoints = [], _tempPoints2 = [], $this = this,
                    _handler = new Cesium.ScreenSpaceEventHandler(this._viewer.scene.canvas);

                // 高度
                function _getHeading(startPosition, endPosition) {
                    if (!startPosition && !endPosition) return 0
                    if (Cesium.Cartesian3.equals(startPosition, endPosition)) return 0
                    let cartographic = Cesium.Cartographic.fromCartesian(startPosition);
                    let cartographic2 = Cesium.Cartographic.fromCartesian(endPosition);
                    return (cartographic2.height - cartographic.height).toFixed(2)
                }

                // 偏移点
                function _computesHorizontalLine(positions) {
                    let cartographic = Cesium.Cartographic.fromCartesian(positions[0]);
                    let cartographic2 = Cesium.Cartographic.fromCartesian(positions[1]);
                    return Cesium.Cartesian3.fromDegrees(
                        Cesium.Math.toDegrees(cartographic.longitude),
                        Cesium.Math.toDegrees(cartographic.latitude),
                        cartographic2.height
                    )
                }

                // left
                _handler.setInputAction(function (movement) {

                    var position = $this.getCatesian3FromPX(movement.position);
                    if (!position && !position.z) return false
                    if (_positions.length == 0) {
                        _positions.push(position.clone())
                        _positions.push(position.clone())
                        _tempPoints.push(position.clone())
                        _tempPoints.push(position.clone())
                    } else {
                        _handler.destroy();
                        if (typeof options.callback === 'function') {

                            options.callback({e: _trianglesEntity, e2: _tempLineEntity, e3: _tempLineEntity2});
                        }
                    }
                }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
                // mouse
                _handler.setInputAction(function (movement) {

                    var position = $this.getCatesian3FromPX(movement.endPosition);
                    if (position && _positions.length > 0) {
                        //直线
                        _positions.pop()
                        _positions.push(position.clone());
                        let horizontalPosition = _computesHorizontalLine(_positions)
                        //高度
                        _tempPoints.pop()
                        _tempPoints.push(horizontalPosition.clone())
                        //水平线
                        _tempPoints2.pop(), _tempPoints2.pop()
                        _tempPoints2.push(position.clone())
                        _tempPoints2.push(horizontalPosition.clone())
                    }
                }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)

                // create entity

                //直线
                _trianglesEntity.polyline = {
                    positions: new Cesium.CallbackProperty(function () {
                        return _positions
                    }, false),
                    ...options.style
                }
                _trianglesEntity.position = new Cesium.CallbackProperty(function () {
                    return _positions[0]
                }, false)
                _trianglesEntity.point = {
                    pixelSize: 5,
                    outlineColor: Cesium.Color.BLUE,
                    outlineWidth: 5
                }
                _trianglesEntity.label = {
                    text: new Cesium.CallbackProperty(function () {
                        return '直线:' + $this.getPositionDistance($this.transformCartesianArrayToWGS84Array(_positions)) + '米'
                    }, false),
                    show: true,
                    showBackground: true,
                    font: '14px monospace',
                    horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
                    verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                    pixelOffset: new Cesium.Cartesian2(50, -100) //left top
                }
                //高度
                _tempLineEntity.polyline = {
                    positions: new Cesium.CallbackProperty(function () {
                        return _tempPoints
                    }, false),
                    ...options.style
                }
                _tempLineEntity.position = new Cesium.CallbackProperty(function () {
                    return _tempPoints2[1]
                }, false)
                _tempLineEntity.point = {
                    pixelSize: 5,
                    outlineColor: Cesium.Color.BLUE,
                    outlineWidth: 5
                }
                _tempLineEntity.label = {
                    text: new Cesium.CallbackProperty(function () {
                        return '高度:' + _getHeading(_tempPoints[0], _tempPoints[1]) + '米'
                    }, false),
                    show: true,
                    showBackground: true,
                    font: '14px monospace',
                    horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
                    verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                    pixelOffset: new Cesium.Cartesian2(-20, 100) //left top
                }
                //水平
                _tempLineEntity2.polyline = {
                    positions: new Cesium.CallbackProperty(function () {
                        return _tempPoints2
                    }, false),
                    ...options.style
                }
                _tempLineEntity2.position = new Cesium.CallbackProperty(function () {
                    return _positions[1]
                }, false)
                _tempLineEntity2.point = {
                    pixelSize: 5,
                    outlineColor: Cesium.Color.BLUE,
                    outlineWidth: 5
                }
                _tempLineEntity2.label = {
                    text: new Cesium.CallbackProperty(function () {
                        return '水平距离:' + $this.getPositionDistance($this.transformCartesianArrayToWGS84Array(_tempPoints2)) + '米'
                    }, false),
                    show: true,
                    showBackground: true,
                    font: '14px monospace',
                    horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
                    verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                    pixelOffset: new Cesium.Cartesian2(-150, -20) //left top
                }
                this._drawLayer.entities.add(_tempLineEntity2)
                this._drawLayer.entities.add(_tempLineEntity)
                this._drawLayer.entities.add(_trianglesEntity)
            }
        },

        /**
         * 测高度
         * */
        //****************************高度测量 第一个点的经纬度,第二个点的高度,两点水平距离为半径************************************************//
        drawHeightMeasureGraphics: function () {
            let viewer = this._viewer;
            let $this = this;
            var _lineEntity = new Cesium.Entity();
            // var handler = new Cesium.ScreenSpaceEventHandler(this._viewer.scene.canvas);
            $this.handler = new Cesium.ScreenSpaceEventHandler(this._viewer.scene.canvas);
            var positions = [];
            var poly = null;
            var tooltip = document.getElementById("toolTip");
            var height = 0;
            var cartesian = null;
            tooltip.style.display = "block";

            $this.handler.setInputAction(function (movement) {
                tooltip.style.left = movement.endPosition.x + 3 + "px";
                tooltip.style.top = movement.endPosition.y - 25 + "px";
                tooltip.style.color = '#ffffff'
                tooltip.innerHTML = '<p>单击左键开始,单击右键结束</p>';
                cartesian = viewer.scene.pickPosition(movement.endPosition);
                if (positions.length >= 2) {
                    if (!Cesium.defined(poly)) {
                        poly = new PolyLinePrimitive(positions);
                    } else {
                        positions.pop();
                        positions.push(cartesian);
                    }
                    height = getHeight(positions);
                }
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

            $this.handler.setInputAction(function (movement) {
                tooltip.style.display = "none";
                cartesian = viewer.scene.pickPosition(movement.position);
                if (positions.length === 0) {
                    positions.push(cartesian.clone());
                    positions.push(cartesian);
                    _lineEntity.position = positions[0]
                    _lineEntity.point = {
                        pixelSize: 5,
                        color: Cesium.Color.RED,
                        outlineColor: Cesium.Color.WHITE,
                        outlineWidth: 2,
                        heightReference: Cesium.HeightReference.none
                    }
                    _lineEntity.label = {
                        text: '0米',
                        font: '18px sans-serif',
                        fillColor: Cesium.Color.GOLD,
                        style: Cesium.LabelStyle.FILL_AND_OUTLINE,
                        outlineWidth: 2,
                        verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                        pixelOffset: new Cesium.Cartesian2(20, -20)
                    }
                    $this._drawLayer.entities.add(_lineEntity)
                }

            }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

            $this.handler.setInputAction(function (movement) {
                $this.handler.destroy();
                //positions.pop();//清除移动点
                tooltip.style.display = "none";
                //viewer_g.entities.remove(floatingPoint);
                // console.log(positions);
                //在三维场景中添加Label

                var textDisance = height + "米";

                var point1cartographic = Cesium.Cartographic.fromCartesian(positions[0]);
                var point2cartographic = Cesium.Cartographic.fromCartesian(positions[1]);
                var point_temp = Cesium.Cartesian3.fromDegrees(Cesium.Math.toDegrees(point1cartographic.longitude), Cesium.Math.toDegrees(point1cartographic.latitude), point2cartographic.height);
                _lineEntity.position = point_temp//positions[1]
                _lineEntity.point = {
                    pixelSize: 5,
                    color: Cesium.Color.RED,
                    outlineColor: Cesium.Color.WHITE,
                    outlineWidth: 2,
                    heightReference: Cesium.HeightReference.none
                }
                _lineEntity.label = {
                    text: textDisance,
                    font: '18px sans-serif',
                    fillColor: Cesium.Color.GOLD,
                    style: Cesium.LabelStyle.FILL_AND_OUTLINE,
                    outlineWidth: 2,
                    verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                    pixelOffset: new Cesium.Cartesian2(20, -20)
                }
                $this._drawLayer.entities.add(_lineEntity)
            }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);

            function getHeight(_positions) {
                var cartographic = Cesium.Cartographic.fromCartesian(_positions[0]);
                var cartographic1 = Cesium.Cartographic.fromCartesian(_positions[1]);
                var height_temp = cartographic1.height - cartographic.height;
                return height_temp.toFixed(2);
            }

            var PolyLinePrimitive = (function () {
                function _(positions) {
                    this.options = {
                        name: '直线',
                        polyline: {
                            show: true,
                            positions: [],
                            material: Cesium.Color.AQUA,
                            width: 2
                        },
                        ellipse: {
                            show: true,
                            // semiMinorAxis : 30.0,
                            // semiMajorAxis : 30.0,
                            // height: 20.0,
                            material: Cesium.Color.GREEN.withAlpha(0.5),
                            outline: true // height must be set for outline to display
                        }
                    };
                    this.positions = positions;
                    this._init();
                }

                _.prototype._init = function () {
                    var _self = this;
                    var _update = function () {
                        var temp_position = [];
                        temp_position.push(_self.positions[0]);
                        var point1cartographic = Cesium.Cartographic.fromCartesian(_self.positions[0]);
                        var point2cartographic = Cesium.Cartographic.fromCartesian(_self.positions[1]);
                        var point_temp = Cesium.Cartesian3.fromDegrees(Cesium.Math.toDegrees(point1cartographic.longitude), Cesium.Math.toDegrees(point1cartographic.latitude), point2cartographic.height);
                        temp_position.push(point_temp);
                        console.log(temp_position);
                        return temp_position;
                    };
                    var _update_ellipse = function () {
                        return _self.positions[0];
                    };
                    var _semiMinorAxis = function () {
                        var point1cartographic = Cesium.Cartographic.fromCartesian(_self.positions[0]);
                        var point2cartographic = Cesium.Cartographic.fromCartesian(_self.positions[1]);
                        /**根据经纬度计算出距离**/
                        var geodesic = new Cesium.EllipsoidGeodesic();
                        geodesic.setEndPoints(point1cartographic, point2cartographic);
                        var s = geodesic.surfaceDistance;
                        return s;
                    };
                    var _height = function () {
                        var height_temp = getHeight(_self.positions);
                        return height_temp;
                    };
                    //实时更新polyline.positions
                    this.options.polyline.positions = new Cesium.CallbackProperty(_update, false);
                    this.options.position = new Cesium.CallbackProperty(_update_ellipse, false);
                    this.options.ellipse.semiMinorAxis = new Cesium.CallbackProperty(_semiMinorAxis, false);
                    this.options.ellipse.semiMajorAxis = new Cesium.CallbackProperty(_semiMinorAxis, false);
                    this.options.ellipse.height = new Cesium.CallbackProperty(_height, false);
                    $this._drawLayer.entities.add(this.options);
                };

                return _;
            })();
        },
        /**
         * 清除测量
         * @param {*}
         */
        clear: function () {
            this._drawLayer.entities.removeAll()
        },
        moveHandle:function(){
            if(this._handlers){
                this._handlers.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
                this._handlers = null
            }  
        }
    }
    return _
})(Cesium);