【Mars3d】new mars3d.layer.GeoJsonLayer({不规则polygon加载label不在正中间的解决方案

问题:

1.new mars3d.layer.GeoJsonLayer({type: "polygon",在styleOptions里配置label的时候,发现这个

不规则polygon加载的时候,会出现label不在中心位置。

graphicLayer = new mars3d.layer.GeoJsonLayer({

name: "全国省界",

url: "//data.mars3d.cn/file/geojson/areas/100000_full.json",

format: simplifyGeoJSON, // 用于自定义处理geojson

symbol: {

type: "polylineP",

styleOptions: {

width: 2,

materialType: mars3d.MaterialType.LineFlow,

materialOptions: {

color: "#00ffff",

image: "img/textures/fence-line.png",

speed: 10,

repeat_x: 10

},

distanceDisplayCondition: true,

distanceDisplayCondition_far: 12000000,

distanceDisplayCondition_near: 100000,

label: {

text: "{name}",

position: "{center}", // 省会位置center

font_size: 30,

color: "#ffffff",

outline: true,

outlineColor: "#000000",

scaleByDistance: true,

scaleByDistance_far: 60000000,

scaleByDistance_farValue: 0.2,

scaleByDistance_near: 1000000,

scaleByDistance_nearValue: 1,

distanceDisplayCondition: true,

distanceDisplayCondition_far: 10000000,

distanceDisplayCondition_near: 100000,

setHeight: 10000

}

}

},

flyTo: true

})

map.addLayer(graphicLayer)

label: {

text: "{name}",

position: "{center}", // 省会位置center

这个配置可以看到在不规则的时候,就无法显示在中心了,是因为sdk内部计算多个点的点位中心依靠的是数学的围合面的多个点的中心点坐标算法计算的。

相关链接:

PolyUtil - V3.6.18 - Mars3D API文档

翻看了mars3d.graphic.PolygonEntity.StyleOptions,面 支持的样式信息这个label的文字所在位置除了center还有其他的位置是可以额外支持的。

解决方案:

可以在我们的图形数据里面加properties,带着坐标点位,就类似我们自己维护的数据一样,加个参数是专门显示点位的。

真正在项目中遇到的不规则的面数据也没几个,可以一个个点一下,避免通过center获取中心点有时候会在图形之外的问题。

默认是中心点位,遇到一个不正确的就修改一个。

相关推荐
Sahas10191 天前
安装 Redis 为系统服务
数据库·redis·缓存
一蓑烟雨*任平生1 天前
【无标题】
数据库
Mahut1 天前
从零构建神经影像可视化库:neuroviz 的架构设计与实现
前端·javascript·github
慧一居士1 天前
VueUse 功能介绍使用场景及完整使用示例
前端·vue.js
奇怪的猫1 天前
浏览器窗口最小化的时候,setInterval 执行变慢,解决方案
前端·javascript
多租户观察室1 天前
工作流新生态:2026年工作流与Coding的重新分工
前端·人工智能·后端·低代码
cmd1 天前
别再混淆了!JS类型转换底层:valueOf vs toString vs Symbol.toPrimitive 详解
前端·javascript
Carsene1 天前
开源项目文档架构设计:Git Submodule 实现文档与代码的优雅分离
前端·后端
霖霖总总1 天前
[Redis小技巧27]Redis Cluster 全景指南:Gossip 协议、故障转移与生产避坑实战
数据库·redis·缓存
Z思学1 天前
promise 有几种状态 async/await 和promise 有什么关系
前端