当前位置:网站首页>Cesium 多边形增加文字标签(polygon 加 label)多边形中心点偏移问题解决
Cesium 多边形增加文字标签(polygon 加 label)多边形中心点偏移问题解决
2022-06-28 00:05:00 【最凶残的小海豹】
之前在网上搜到的获取中心点的方法如下:
// 多边形的坐标集合(如果已经获取到了,就跳过这一步)
var polygon_point_arr = polygon_point_entity.polygon.hierarchy.getValue(Cesium.JulianDate.now()).positions;
// 根据坐标集合构造BoundingSphere获取中心点坐标
let center_point = Cesium.BoundingSphere.fromPoints(polygon_point_arr).center
// 设置点标记的坐标(这一步将中心点转到地球表面)
polygon_point_entity.position = Cesium.Ellipsoid.WGS84.scaleToGeodeticSurface(center_point);
// 添加点标记
polygon_point_entity.label = {
// 点标记参数
}
但是如果多边形不太规则或者点比较多的情况下,标记的位置就不在中心点了,会有偏移,例如下图
原因:boundingsphere 计算的中心点是外接圆的圆心,所以导致了中心点会在图形外。
相关资料参考的是:https://www.lmlphp.com/user/58093/article/item/771705/
解决方案: 使用 turf.js 来计算。
turf.js 中文网:https://turfjs.fenxianglu.cn/
代码:turf.js 版本是 6.5.0
// 多边形的坐标集合(如果已经获取到了,就跳过这一步)
var polygon_point_arr = polygon_point_entity.polygon.hierarchy.getValue(Cesium.JulianDate.now()).positions;
// 保存转换后的点数组,这个格式必须按照 turf 的要求来
let turf_arr = [[]];
// 坐标转换
polygon_point_arr.forEach(val => {
let polyObj = {
}
// 空间坐标转世界坐标(弧度) 同 Cesium.Cartographic.fromCartesian
let cartographic = gvEarth.scene.globe.ellipsoid.cartesianToCartographic(val)
// 弧度转为角度(经纬度)
polyObj.lon = Cesium.Math.toDegrees(cartographic.longitude)
polyObj.lat = Cesium.Math.toDegrees(cartographic.latitude)
turf_arr[0].push([polyObj.lon, polyObj.lat])
})
// turf 需要将整个点闭合,所以最后一个点必须和起点重合。
turf_arr[0].push(turf_arr[0][0])
let turf_position = turf.polygon(turf_arr)
let turf_position_point = turf.centerOfMass(turf_position)
// 设置点标记坐标
polygon_point_entity.position = Cesium.Cartesian3.fromDegrees(turf_position_point.geometry.coordinates[0], turf_position_point.geometry.coordinates[1], 0)
// 添加点标记
polygon_point_entity.label = {
// 点标记参数
}

边栏推荐
- 药物发现综述-02-分子性质预测
- 指南针股票开户是会有什么风险吗?指南针开户安全吗
- OS模块与OS.path 模块的学习
- Adobe Premiere Basics - common video effects (corner positioning, mosaic, blur, sharpen, handwriting tools, effect control hierarchy) (16)
- [elt.zip] openharmony paper Club - memory compression for data intensive applications
- PostgreSQL setting auto increment field
- Solon 1.8.3 发布,云原生微服务开发框架
- Chapitre 4: redis
- 【ELT.ZIP】OpenHarmony啃论文俱乐部—数据密集型应用内存压缩
- Set collection usage
猜你喜欢

评价——灰色关联分析

嵌入式必学,硬件资源接口详解——基于ARM AM335X开发板 (上)
![[embedded foundation] memory (cache, ram, ROM, flash)](/img/bd/d952ebd4033a993dd16cd792bb3e1e.png)
[embedded foundation] memory (cache, ram, ROM, flash)

How fiddle uses agents

Adobe Premiere Basics - common video effects (corner positioning, mosaic, blur, sharpen, handwriting tools, effect control hierarchy) (16)

【嵌入式基础】串口通信

将某数据库N多表名作为另外一张表中某一列得值(范围可以是别的数据库中得某张表)

Original | 2025 to achieve the "five ones" goal! The four products of Jiefang power are officially released

Google Earth engine (GEE) -- an error caused by the imagecollection (error) traversing the image collection

What problems should be evaluated before implementing MES management system
随机推荐
TD Hero 线上发布会|7月2日邀你来
The number of nodes of a complete binary tree [non-O (n) solution > Abstract dichotomy]
Drug interaction prediction based on learning size adaptive molecular substructure
[Yocto RM]1 - System Requirements
COSCon'22 讲师征集令
Review of drug discovery-03-molecular design and optimization
frp实现内网穿透
MySQL十种锁,一篇文章带你全解析
对比学习中的4种经典训练模式
Arrays. Aslist() pit
[sylixos] I2C device driver creation and use
Is there any risk in opening an account for flush stock? Is it safe for flush to open an account
Scala 基础 (三):运算符和流程控制
Interview guide for data person | prepare these points to be prepared!
PostgreSQL setting auto increment field
Want to open an account to buy stock, is it safe to open an account on the Internet?
【sylixos】NEW_1 型字符驱动示例
自监督学习与药物发现
pytorch_ lightning. utilities. exceptions. MisconfigurationException: You requested GPUs: [1] But...
Adobe Premiere Basics - common video effects (cropping, black and white, clip speed, mirroring, lens halo) (XV)