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

Calling map.setStyle() removes all layers and sources #8660

Closed
Japjappedulap opened this issue Aug 20, 2019 · 6 comments
Closed

Calling map.setStyle() removes all layers and sources #8660

Japjappedulap opened this issue Aug 20, 2019 · 6 comments

Comments

@Japjappedulap
Copy link

mapbox-gl-js version: 1.2.1

browser: Chrome(76.0.3809.100) and Firefox(68.0.2)

Steps to Trigger Behavior

  1. Initialise map
  2. Add multiple sources, layers
  3. Call setStyle()
  4. All added sources and layers get removed

Link to Demonstration

This can be seen in this official example.

I've also added a basic example derived from the setStyle example which only adds another layer to the map. If you change the map style, this layer will be removed.

https://jsbin.com/yekisosehe/1/edit?html,console,output

Expected Behavior

Change the map style and do NOT remove all the existing layers. This is critical, as in my case, I have a great deal of layers, sources and markers on the map and removing them when switching styles is a deal breaker.

Actual Behavior

Changes the map style and generates a warning in the console.

Unable to perform style diff: Unimplemented: setSprite..  Rebuilding the style from scratch.

Any known way to work around this and not rerender all the layers? Maybe I'm missing something obvious, but I don't think this should be the behaviour. I know there are some related issues such as 8241, 2058 or 2059 but with no conclusive answers.

@andrewharvey
Copy link
Collaborator

I believe this is #4006. That thread has some discussion on possible solutions, but generally the expectation is with you to either re-add those sources/layers after setStyle or even better integrate them into your Style JSON you pass to setStyle so that the smart diff which powers setStyle can retain them.

@mourner
Copy link
Member

mourner commented Aug 20, 2019

@andrewharvey thanks for chiming in, seconded. Closing as a duplicate of #4006.

@mourner mourner closed this as completed Aug 20, 2019
@BlackHandguy
Copy link

do you solve the question?

@InfoseedDeveloper
Copy link

Create one function that adds an existing layer and source again.
Import the style into map.setStyle() and then access sources and layers to add it again.

The function execution must use setTimeout. Otherwise not applicable
In my case, I gave 200

@itanka9
Copy link

itanka9 commented Dec 4, 2022

@InfoseedDeveloper

Sometimes setTimeout() may become flaky, e.g. in case of slow network or device. AFAIR a little bit more correct way is wait for styledata event which fired after map completes style loading.

@NessinM
Copy link

NessinM commented Aug 9, 2023

Here I leave a possible solution, I hope it can help you. codesandbox.io

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

7 participants