Search code examples
angularhtml-listsmapboxopenlayersmapbox-studio

how to remove a ol-mapbox-style layer loaded with apply method


Since most of the times I add and remove layers by using addLayer() and removeLayer() OL methods, how can I handle a vector layer handled with ol-mapbox-style similarly? The following angular code is not working and I dont see a suitable way in https://github.com/openlayers/ol-mapbox-style.

changeToVectorTileMapTilerEmbebedJson() {
  apply(
    this.map,
    'https://api.maptiler.com/maps/b3265770-0173-4415-909d-264ef9934779/style.json?key=blablabla'
  )
  .then(() => this.map.removeLayer(this.stamenTerrain))
  .then(() => this.map.removeLayer(this.stamenWaterColor))
  .then(() => this.map.removeLayer(this.osm))
  .then(() => this.map.removeLayer(this.topMap))
  .then(() => this.map.removeLayer(this.vectorTileMapTilerHillShades))
  .then(() => this.map.removeLayer(this.vectorTileMapTilerSatMediumbres))
  .then(() => this.map.removeLayer(this.vectorTileArcGISpbf))

}

Thank you


Solution

  • You can identify layers created by ol-mapbox-style because they have mapbox-source and mapbox-layers properties. If you filter the layers array on either of those you can remove those layers

    this.map.getlayers().getArray().filter(
      layer => return layer.get('mapbox-source');
    ).forEach(
      layer => this.map.removeLayer(layer);
    );