cesium解决DeveloperError报错‘Expected longitude to be typeof number, actual type of was string‘


Cesium实战系列文章总目录传送门

1.出现问题

背景:在进行Cesium项目开发时,将Cesium.js版本从压缩版本转换为未压缩版本(便于开发调试)时就会出现DeveloperError报错Expected longitude to be typeof number, actual type of was string
即只是切换Cesium.js版本,未做其他任何更改。
在这里插入图片描述

2.解决方法

看报错信息可以发现是check.js在进行类型检查时报错,即要求是数字类型,但实际是字符串。但是为什么压缩版的Cesium.js没有报错?这里猜想可能是没有进行类型检查。

2.1确定报错位置

查看报错信息可知,报错位置是在使用primitive方式加载geojson数据绘制墙体上。

2.2对症修改

(1)报错说类型应该为number,而非string,那就将string转换为number

let positionArray = [];
// 将字符串数组polyonArray转换为数字数组positionArray
for (element of polygonArray) {
    positionArray.push(Number(element));
}

(2)将字符串转换为数字后,WallGeometry.js又报错options.positions and options.maximumHeights must have the same length
在这里插入图片描述
即墙体的高度数组应该和底部坐标数组的长度一致。设置代码如下:

const geometry = new Cesium.WallGeometry({
    positions: Cesium.Cartesian3.fromDegreesArray(positionArray),
    // 设置高度
    maximumHeights: new Array(positionArray.length / 2).fill(50),
    minimunHeights: new Array(positionArray.length / 2).fill(0),

})

(3)修改后即可正确显示。
整体函数代码:

// 使用primitive方式加载geojson
let urlPath = './data/park/parks.geojson';
// 使用jQuery异步加载json数据
$.get(urlPath, function(data) {
    const features = data.features;
    addDataToGlobe(features);
})

function addDataToGlobe(features) {
    const instances = [];
    for (let i = 0; i < features.length; i++) {
        for (let j = 0; j < features[i].geometry.coordinates.length; j++) {
            // 将字符串按照","拆分为数组
            polygonArray = features[i].geometry.coordinates[j].toString().split(',');
        }

        let positionArray = [];
        // 将字符串数组转换为数字数组
        for (element of polygonArray) {
            positionArray.push(Number(element));
        }
        const geometry = new Cesium.WallGeometry({
            positions: Cesium.Cartesian3.fromDegreesArray(positionArray),
            // 设置高度
            maximumHeights: new Array(positionArray.length / 2).fill(50),
            minimunHeights: new Array(positionArray.length / 2).fill(0),

        })
        instances.push(new Cesium.GeometryInstance({
            geometry: geometry,
            attributes: {
                color: Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.GREEN),
            },
        }));
    }
    // 合并单个geometry,提高渲染效率
    const primitive = new Cesium.Primitive({
        geometryInstances: instances,
        // PerInstanceColorAppearance:使用每个实例自定义的颜色着色
        appearance: new Cesium.PerInstanceColorAppearance(),
    });
    this.viewer.scene.primitives.add(primitive);
}

修改后正确运行绘制的墙体效果:
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

右弦GISer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值