Skip to content

How do I clear map from previous style applied #1194

Answered by ahocevar
daniluk4000 asked this question in Q&A
Discussion options

You must be logged in to vote

The problem is that https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json has

  "id": "voyager"

and https://basemaps.cartocdn.com/gl/voyager-gl-style/style.json also has

  "id": "voyager"

ol-mapbox-style caches styles by id to allow for efficient theme switching. If both themes have the same id, that won't work. A workaround is to set the id to the style's url:

  group = new LayerGroup();
  const url = themes[currentTheme];
  fetch(url)
    .then((r) => r.json())
    .then((json) => {
      json.id = url;
      apply(group, json);
      map.addLayer(group);
    });

Here is a working codesandbox with theme switching: https://codesandbox.io/p/sandbox/8nsy64

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@daniluk4000
Comment options

Answer selected by daniluk4000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants