walkermatt / ol-layerswitcher

Layer control for OpenLayers
MIT License
384 stars 176 forks source link

How to show layers added by ol-mapbox-style #456

Closed geraldo closed 3 years ago

geraldo commented 3 years ago

I can't figure out how to add layers added by ol-mapbox-style olms function. I tried to add a "title" parameter to mapbox style json, but this get's ignored. So maybe I could manually add it to LayerSwitcher once the layer has beed added to the OpenLayers map instance? olms function returns a promise when everything is done:

olms(map, 'style.json').then(() => {
  // now I can access the layer -> add to LayerSwitcher
});
walkermatt commented 3 years ago

Hi, see https://github.com/walkermatt/ol-layerswitcher/issues/397#issuecomment-778269806

geraldo commented 3 years ago

Great, using setProperties to the mapbox layer solved my issue:

olms(map, 'style.json').then(() => {
    const mbLayer = map.getLayers().getArray().find(layer => layer.get('mapbox-source') === 'mbSource');
    mbLayer.setProperties({
        title: 'MapBox'
    });
});