码迷,mamicode.com
首页 > 其他好文 > 详细

Cesium 学习小结(一)

时间:2020-03-11 15:29:10      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:log   des   bytes   sphere   text   数据解析   parser   ati   for   

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(/[\r\n]/g, "").split(‘ ‘);
                var coords = $(feature).find(coordinates);
                let coordCollection = [];
                for (let i = 0; i < coords.length; i++) {
                    let coordinate = $(coords[i]).text().trim().replace(/[\r\n]/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 学习小结(一)

标签:log   des   bytes   sphere   text   数据解析   parser   ati   for   

原文地址:https://www.cnblogs.com/amadoGrowers/p/12462334.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!