【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获取中心点有时候会在图形之外的问题。

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

相关推荐
代码搬运媛6 小时前
Jest 测试框架详解与实现指南
前端
counterxing6 小时前
我把 Codex 里的 Skills 做成了一个 MCP,还支持分享
前端·agent·ai编程
wangqiaowq7 小时前
windows下nginx的安装
linux·服务器·前端
之歆7 小时前
DAY_12JavaScript DOM 完全指南(二):实战与性能篇
开发语言·前端·javascript·ecmascript
星星也在雾里7 小时前
PgBouncer 解决 PostgreSQL 连接数超限 + 可视化监控
数据库·postgresql
发现一只大呆瓜7 小时前
Vite凭什么这么快?3分钟带你彻底搞懂 Vite 热更新的幕后黑手
前端·面试·vite
Maimai108087 小时前
React如何用 @microsoft/fetch-event-source 落地 SSE:比原生 EventSource 更灵活的实时推送方案
前端·javascript·react.js·microsoft·前端框架·reactjs·webassembly
cen__y8 小时前
Linux12(Git01)
linux·运维·服务器·c语言·开发语言·git
雨辰AI9 小时前
SpringBoot3 + 人大金仓读写分离 + 分库分表 + 集群高可用 全栈实战
java·数据库·mysql·政务
长城20249 小时前
关于MySql的ONLY_FULL_GROUP_BY问题
数据库·mysql·聚合列