【问题标题】:How to get MapLibre Layers properties?如何获取 MapLibre 图层属性?
【发布时间】:2022-01-03 16:11:30
【问题描述】:

请帮忙,我已经受苦 5 天了。 如何获取MapLibre 地图的所有layer Ids

function addLayer(map, options, layer) {
    let currentLayer = edges_data_api.find(
    element => element.edge_id === layer.feature.properties.edge_id)
    map.setPaintProperty('lines', 'fill-color', ['interpolate', ['linear'],
    ['get', currentLayer.lanes], 0, 'rgb(255, 255, 255)', 5, 'rgb(255, 0, 0)'])
    }

这是我使用MapLibre 定义地图的方式

map.on('load', function() {
    map.addSource('lines', {
        type: 'geojson',
        data: data
    });

   map.addLayer({
        'id': 'lines',
        'type': 'fill',
        'source': 'lines',
        'layout': {},
        'paint': {
        'fill-color': '#4682B4',
        'fill-opacity': 0.8,
        }
    });
   map.setPaintProperty('lines', 'fill-color', ['get', 'color'])
})

【问题讨论】:

    标签: javascript leaflet mapbox maplibre-gl


    【解决方案1】:

    据我所知,getAllLayers() 没有 api。所以你应该保留你自己的图层ID列表。

    //global variable
    const layerIds = new Set()
    
    map.on('load', function() {
    
        layerIds.add('lines') //keep list of ids without duplicates
    
        map.addSource('lines', {
            type: 'geojson',
            data: data
        });
    
       map.addLayer({
            'id': 'lines',
            'type': 'fill',
            'source': 'lines',
            'layout': {},
            'paint': {
            'fill-color': '#4682B4',
            'fill-opacity': 0.8,
            }
        });
       map.setPaintProperty('lines', 'fill-color', ['get', 'color'])
    })
    
    
    //You can iterate your list ids where it is needed
    layerIds.forEach(value => {//action here})
    

    【讨论】:

    • 非常感谢。它不工作。当我 console.log(value) 时我什么都没有
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    相关资源
    最近更新 更多