美文网首页开源
cesium 添加ploygon方法

cesium 添加ploygon方法

作者: 宿州刘德华 | 来源:发表于2019-03-26 17:21 被阅读1次
function drawPolygon(callback){
        var _this = this;
        var PolygonPrimitive = (function () {
            function _(positions) {
                this.options = {
                    //name: '多边形',
                    polygon: {
                        hierarchy: [],
                        perPositionHeight: true,
                        //material: Cesium.Color.RED.withAlpha(0.4),
                        //extrudedHeight: 50.0,//高度
                        material : Cesium.Color.BLUE.withAlpha(0.4)
                    }
                };
                this.hierarchy = positions;
                this._init();
            }

            _.prototype._init = function () {
                var _self = this;
                var _update = function () {
                    return _self.hierarchy;
                };
                //实时更新polygon.hierarchy
                this.options.polygon.hierarchy = new Cesium.CallbackProperty(_update, false);
                _this.viewer.entities.add(this.options);
            };
            return _;
        })();

        var handler = new Cesium.ScreenSpaceEventHandler(_this.viewer.scene.canvas);
        var positions = [];
        var poly = undefined;

        //鼠标单击画点
        handler.setInputAction(function (movement) {
            var cartesian = _this.viewer.scene.camera.pickEllipsoid(movement.position, _this.viewer.scene.globe.ellipsoid);
            if (positions.length == 0) {
                positions.push(cartesian.clone());
            }
            positions.push(cartesian);
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
        //鼠标移动
        handler.setInputAction(function (movement) {
            var cartesian = _this.viewer.scene.camera.pickEllipsoid(movement.endPosition, _this.viewer.scene.globe.ellipsoid);
            if (positions.length >= 2) {
                if (!Cesium.defined(poly)) {
                    poly = new PolygonPrimitive(positions);
                } else {
                    if(cartesian != undefined){
                        positions.pop();
                        cartesian.y += (1 + Math.random());
                        positions.push(cartesian);
                    }
                }
            }
        }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
        //鼠标右键单击结束绘制
        handler.setInputAction(function (movement) {
            handler.destroy();
           // callback(positions);
        }, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
}

相关文章

网友评论

    本文标题:cesium 添加ploygon方法

    本文链接:https://www.haomeiwen.com/subject/oujnvqtx.html