cesium 添加ploygon方法

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  return new Cesium.PolygonHierarchy(_self.hierarchy);//_self.hierarchy;
                };
                //實(shí)時更新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;

        //鼠標(biāo)單擊畫點(diǎn)
        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);
        //鼠標(biāo)移動
        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);
        //鼠標(biāo)右鍵單擊結(jié)束繪制
        handler.setInputAction(function (movement) {
            handler.destroy();
           // callback(positions);
        }, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容