Cesium 学习小结

Posted amadogrowers

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cesium 学习小结相关的知识,希望对你有一定的参考价值。

1.加载3dtile数据(url是3dtile数据配置文件,proxy可能存在跨域所需要的代理) 

        //加载3dtiles
        function add3DTiles(url, proxy) {
            var tileset = viewer.scene.primitives.add(new Cesium.Cesium3DTileset({
                url: new Cesium.Resource({
                    url: url,
                    proxy: new Cesium.DefaultProxy(proxy)
                })
            }));
            return tileset;
        }

2.3dtile单体化

viewer是 var viewer = new Cesium.Map("cesiumContainer")的初始化球体

dytTiles是上述加载完成的3dtile数据

data是单体化数据,遵循geojson格式
        //单体化3d tiles
        function dth_3dtiles(data) {
            tilesetMonomer_dyt = new Cesium.Cesium3DTilesetMonomer({
                viewer: viewer,
                tileset: dytTiles,
                moveColor: Cesium.Color.fromBytes(255, 50, 50, 122),//移动时的颜色,红色
                selectedColor: Cesium.Color.fromBytes(50, 255, 50, 122),//选中的颜色,绿色
                source: { type: "geojson", data: data }
            });
            //选中事件,回调
            tilesetMonomer_dyt.seletedEvent.addEventListener(function (feature) {
                console.log(feature.id.description);
            }, tilesetMonomer_dyt);
        }

3.利用加载的3dtile数据定位

        //定位
        function location3DTiles(flag) {
            var tileset = null;
            if (flag == "dyt") {
                tileset = dytTiles;
            }
            viewer.camera.viewBoundingSphere(tileset.boundingSphere, new Cesium.HeadingPitchRange(0, -1, 0));
            viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
        }

4.将xml解析并序列化为Object对象

    //将xml解析并序列化为Object对象
        function parserFeature(res) {
            var features = parserXml(res);
            var length = features.length;
            var featureCollection = [];
            for (var i = 0; i < length; i++) {
                var feature = features[i];
                var attribute = $(feature).children().children();
                var properties = parserAttribute(attribute);
                var type;
                try {
                    type = $(attribute[1]).children()[0].nodeName;
                }
                catch (e) {
                    attribute = $(feature).children();
                    properties = parserAttribute(attribute);
                    type = $(attribute[1]).children()[0].nodeName;
                }
                //var coords = $(feature).find(‘coordinates‘).text().trim().replace(/[
]/g, "").split(‘ ‘);
                var coords = $(feature).find(coordinates);
                let coordCollection = [];
                for (let i = 0; i < coords.length; i++) {
                    let coordinate = $(coords[i]).text().trim().replace(/[
]/g, "").split( );
                    var coordinates = [];
                    coordinate.forEach(function (c) {
                        if (c != ‘‘) {
                            var xy = c.split(,);
                            coordinates.push([Number(xy[0]), Number(xy[1])]);
                        }
                    });
                    coordCollection.push(coordinates);
                }
                //continue;
                //   type = titleCase(type);
                switch (type) {
                    case "POLYGON":
                        type = Polygon;
                        break;
                    case "MULTIPOLYGON":
                        type = MultiPolygon;
                        break;
                }
                featureCollection.push({
                    type: "Feature",
                    properties: properties,
                    geometry: {
                        type: type,
                        coordinates: coordCollection
                    }
                });
            }
            return featureCollection;
        }
        //大写变小写,首字符大写
        function titleCase(str) {
            var array = str.toLowerCase().split(" ");
            for (var i = 0; i < array.length; i++) {
                array[i] = array[i][0].toUpperCase() + array[i].substring(1, array[i].length);
            }
            var string = array.join(" ");
            return string;
        }
        //地图服务数据解析
        function parserXml(xml) {
            if (typeof xml == string) {
                var result = xml.replace(/gml:/g, ‘‘);
                var features = $(result).find(featureMember);
                return features;
            }
        }
        //属性数据解析
        function parserAttribute(attribute) {
            var attri = {};
            var len = attribute.length;
            for (var i = 0; i < len; i++) {
                attri[attribute[i].tagName] = attribute[i].textContent;
            }
            return attri;
        }

 

 

以上是关于Cesium 学习小结的主要内容,如果未能解决你的问题,请参考以下文章

cesium 雷达扫描(附源码下载)

Cesium案例解析——HelloWorld

cesium 学习 HeadingPitchRoll

cesium 结合 geoserver 实现地图属性查询(附源码下载)

cesium结合geoserver实现地图空间查询(附源码下载)

cesium 学习 基础绘制(点线面)