gpt4 book ai didi

javascript - 如何从 topojson 文件中获取文本标签以多边形显示?

转载 作者:行者123 更新时间:2023-11-29 23:55:25 26 4
gpt4 key购买 nike

我正在制作一张使用 Leaflet 构建的 map ,并且有一个 topoJSON 文件,该文件是构成欧洲和世界其他地区的国家边界的 featureCollection。要素集合包含每个国家/地区的属性名称。

问题:让国家名称以每个国家的多边形为中心的推荐方法是什么?我应该使用传单的工具提示功能吗?我正在使用 this example作为起点,但无法显示我的文本标签。有任何想法吗?我有一个 topojson 文件,其中包含我试图从中提取的属性。

My fiddle

var earth = L.geoJson();
earth.bindTooltip('text label', {
permanent: true,
direction: 'center'
});


var naturalEarth = omnivore.topojson('https://gist.githubusercontent.com/dosstx/dcd1b4ebe3892527b12759226a21b900/raw/81abff4d455dbabe800c56aa7736dd4cbbfd1f64/topo.json', null, earth);

最佳答案

要使用 Leaflet omnivore 将工具提示绑定(bind)到每一层,请使用 Leaflet's eachLayer function准备就绪后遍历 topojson 中的所有层。

例如

var naturalEarth = omnivore.topojson('https://gist.githubusercontent.com/dosstx/dcd1b4ebe3892527b12759226a21b900/raw/81abff4d455dbabe800c56aa7736dd4cbbfd1f64/topo.json', null, earth)
.on('ready', function() {
naturalEarth.eachLayer(function(layer) {
//console.log(layer); //to inspect what properties are available
layer.bindTooltip(layer.feature.properties.FORMAL_EN, {
permanent: true
});
});
});

更新的 JSFiddle:https://jsfiddle.net/kjLjhbe1/8/

关于javascript - 如何从 topojson 文件中获取文本标签以多边形显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41818470/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com