javascript - 如何将标签添加到 map 框中的多边形

标签 javascript leaflet mapbox

我要在 map 框中的多边形上添加标签,顺便说一句,我找不到如何添加标签的解决方案。

我找到了向点添加标签的方法。

map.addLayer({
    "id": "points",
    "type": "symbol",
    "source": {
        "type": "geojson",
        "data": {
            "type": "FeatureCollection",
            "features": [{
                "type": "Feature",
                "geometry": {
                    "type": "Point",
                    "coordinates": [-77.03238901390978, 38.913188059745586]
                },
                "properties": {
                    "title": "Mapbox DC",
                    "icon": "monument"
                }
            }, {
                "type": "Feature",
                "geometry": {
                    "type": "Point",
                    "coordinates": [-122.414, 37.776]
                },
                "properties": {
                    "title": "Mapbox SF",
                    "icon": "harbor"
                }
            }]
        }
    },
    "layout": {
        "icon-image": "{icon}-15",
        "text-field": "{title}",
        "text-font": ["Open Sans Semibold", "Arial Unicode MS Bold"],
        "text-offset": [0, 0.6],
        "text-anchor": "top"
    }
});

因此,我尝试使用上述解决方案将标签添加到 map 框中的多边形,但失败了。

请帮助我!

最佳答案

您很可能希望将多边形转换为点并将其用作标签源。

您可以直接在应用程序中使用 https://github.com/mapbox/polylabel 执行此操作或事先与 https://github.com/andrewharvey/geojson-polygon-labels

关于javascript - 如何将标签添加到 map 框中的多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49039253/

相关文章:

javascript - 传单 Angular Directive(指令)中的 KML

javascript - leaflet.js : Fire event when setView() has finished the animation. 这怎么可能?

javascript - 基于内部图标的传单簇颜色

javascript - 如何仅在页面上没有事件时自动刷新 HTML?

javascript - RxJs - 具有 Observable 属性的 FlatMap 对象

javascript - 更换节点错误。要替换的节点不是该节点的子节点

leaflet - 将FeatureGroup转换为GeoJson时,Leaflet Draw不采用属性

ios - 还有其他选项可以在 mapbox 中绘制多条路线吗?

ios - 像 uber 一样在 Mapbox map 上移动 MGLPointAnnotation

javascript - 使用 jQuery Deferred 对象解决许多 Backbone 依赖关系