Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look at using style transform function to restore added styles when switching base style #138

Open
dimfeld opened this issue Mar 15, 2024 · 1 comment

Comments

@dimfeld
Copy link
Owner

dimfeld commented Mar 15, 2024

Just spotted maplibre/maplibre-gl-js#2587 (comment), possibly a simpler implementation

Originally posted by @dabreegster in #60 (comment)

Currently we do some tracking of what's been added on top of the base style, and once the new style loads we re-add it. This method wouldn't be too different, but would probably require a bit less bookkeeping and would likely also reduce some flicker since the new base style and the added styles would probably be loaded together.

@selenecodes
Copy link

For user trying to accomplish this right now:

<GeoJSON data={data} id="myprefix-geodata">
  <CircleLayer
    paint={{
      "circle-color": "red",
      "circle-opacity": 0.5,
      "circle-radius": 2,
    }}
  />
</GeoJSON>
map.setStyle(styleObject, {
  transformStyle: (previousStyle, nextStyle) => {
    const custom_layers = previousStyle.layers.filter(layer => layer.source.startsWith('myprefix-'));
    const layers = nextStyle.layers.concat(custom_layers);
    const sources = nextStyle.sources;
    for (const [key, value] of Object.entries(previousStyle.sources)) {
      if (key.startsWith('myprefix-')) {
        sources[key] = value;
      }
    }
    return {
      ...nextStyle,
      sources: sources,
      layers: layers
    };
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants