Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Optionally persist layers and sources across styles #6180

Open

Description

@1ec5

Annotations added to the map persist even after switching styles. This can be especially handy for an application that allows the user to switch back and forth between Streets and Satellite: any dropped pins, drawn shapes, or route lines remain on the map the whole time. The developer isn’t responsible for rebuilding the data on each style change.

Per #5626 (comment), for the runtime styling API to have parity with the annotation API, there would need to be some way for certain layers and sources to optionally persist after switching styles. I’m not sure whether the SDK would have to take full responsibility for persisting this data, or whether mbgl can keep track of some state.

/cc @jfirebaugh @incanus @frederoni

Activity

  1. added
    iOS
    macOS
    annotations
    on Aug 26, 2016
  2. jfirebaugh commented on Aug 26, 2016

    @jfirebaugh
    Contributor

    This is a well-recognized problem, but there appear to be few designs that don't introduce significant additional complexity of their own.

    mapbox/DEPRECATED-mapbox-gl#25 has a proposal for one possible approach.

  3. boundsj commented on Feb 28, 2017

    @boundsj
    Contributor

    For reference, here is a post that illustrates the kind of workaround that platform developers must do to manage runtime styling sources/layers on top of changing styles.

  4. nitrag commented on Feb 28, 2017

    @nitrag
    Contributor

    @1ec5 , Would it be much work for the SDK to remove the source+layers from the mapview right before a style change, keep the references, and add them directly back when the style is ready?

    I think asking the developers to do this isn't efficient and it's not clear in the docs that this WILL happen on style changes.

    I would need to maintain a class level variable for MGLShapeSource to retain the shapes, right?

  5. boundsj commented on Feb 28, 2017

    @boundsj
    Contributor

    @nitrag Would it be much work for the SDK to remove the source+layers from the mapview right before a style change, keep the references, and add them directly back when the style is ready?

    Such an approach might be blocked by #7437

  6. nitrag commented on Mar 7, 2017

    @nitrag
    Contributor

    I discovered a bug with the workaround in #7124 (comment). If you use a class level variable to hold a MGLShapeSource and even though you properly remove the source from the map before changing the style and adding it back to the new style you will get the following crash. Reproduced with aggressive zoom behavior and standard annotations on the map.

    libc++abi.dylib: terminating with uncaught exception of type std::__1::system_error: mutex lock failed: Invalid argument
    

    updateUserLocationAnnotationView
    image

    I've resorted to wiping all the layers clean and starting fresh each time the style changes. Not perfect solution.
    Also, this prevents the use case of being able to record users tracks on a map and being able to change the map style when doing so.

    Please consider this a friendly bump in priority for this feature.

  7. 1ec5 commented on Mar 7, 2017

    @1ec5
    ContributorAuthor

    If I’m not mistaken, yanking a source out from under a layer that uses it could result in undefined behavior. So to be safe, you’d have to remove the layers before removing the source that they use, the change the style, then add the sources and layers back, in that order. I recognize that isn’t a far cry from starting afresh.

  8. anandthakker commented on Mar 7, 2017

    @anandthakker
    Contributor

    Here's a broader tracking ticket for designing a style-spec level solution to this problem: mapbox/mapbox-gl-js#4225

  9. 1ec5 commented on Mar 7, 2017

    @1ec5
    ContributorAuthor

    mapbox/mapbox-gl-js#4225 would only be a complete solution inasmuch as we plan to remove the ability to switch styles altogether, although it would eliminate one use case for switching styles.

  10. nitrag commented on Mar 7, 2017

    @nitrag
    Contributor

    @1ec5, you're right, I was removing source before layer. I've optimized my code but still getting errors

    var recordGeoJSON = MGLShapeSource(identifier: "record-source", shape: nil, options: nil)
    ...
    ...
    func pseudoChangeMapStyle(){
        if let recordS = self.mapView.style?.layer(withIdentifier: "record-style"){
            self.mapView.style?.removeLayer(recordS)
        }
        self.mapView.style?.removeSource(self.recordGeoJSON)
        //also tried:
        /**
        if let recordS = self.mapView.style?.source(withIdentifier: "record-source"){
            self.mapView.style?.removeSource(recordS)
        }
        **/
    
        ...
        self.mapView.styleURL = newStyle
    }
    
    func mapViewDidFinishLoading() {
        ....
        style.addSource(self.recordGeoJSON)
        let recordStyle = MGLLineStyleLayer(identifier: "record-style", source: self.recordGeoJSON)
        ...
        style.addLayer(recordStyle)
        ....
    }
    func updateRecordingLine(_ geojson: JSON){
        if let geojsonData = try? geojson.rawData() {
            self.recordGeoJSON.shape = try? MGLShape(data: geojsonData, encoding: String.Encoding.utf8.rawValue)
        }
        //also tried
        /**
        if let geojsonData = try? geojson.rawData() {
            if let source = self.mapView.style?.source(withIdentifier: "record-source") as? MGLShapeSource{
                source.shape = try? MGLShape(data: geojsonData, encoding: String.Encoding.utf8.rawValue)
            }   
        }
        **/
    }
    func removeRecordingLine(){
        if let source = self.mapView.style?.source(withIdentifier: "record-source") as? MGLShapeSource{
            source.shape = nil
        }
    }

    I'll either get this error:

    Location update received
    offroad2(25727,0x114ad83c0) malloc: *** mach_vm_map(size=11448890837103263744) failed (error code=3)
    *** error: can't allocate region
    *** set a breakpoint in malloc_error_break to debug
    libc++abi.dylib: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
    

    or this one:

    libc++abi.dylib: terminating with uncaught exception of type std::__1::system_error: mutex lock failed: Invalid argument
    

    or a crash in CF_RUN_LOOP.

    I think this is because my location delegate is trying to update self.recordGeoJSON's when it's in transition of ownership phase?

    As you can see above, I've tried self.recordGeoJSON and let source = self.mapView.style?.source(withIdentifier: "record-source") as? MGLShapeSource and they don't show any impact/difference.

    Either way, what a mess.

  11. nitrag commented on Mar 8, 2017

    @nitrag
    Contributor

    I've since removed class level style layers and sources variables and am reloading data on every style change in mapViewDidFinishLoadingMap. While inconvenient, at least it's stable this way. Sorry for the noise. Looking forward to this feature.

    ☀️

  12. 6 remaining items

  13. added
    archived
    on Dec 26, 2018
  14. stale commented on Dec 27, 2018

    @stale

    This issue has been automatically detected as stale because it has not had recent activity and will be archived. Thank you for your contributions.

  15. 1ec5 commented on Mar 8, 2019

    @1ec5
    ContributorAuthor

    Still needed as a prerequisite for #6181.

  16. reopened this on Mar 8, 2019
  17. removed
    archived
    on Mar 8, 2019
  18. songyuyang0918 commented on Jul 12, 2019

    @songyuyang0918

    @1ec5 In mapbox-ios: I wonder if modifying styleURL to preserve layers and sources is supported?
    Is it implemented as a plug-in?
    I've looked at all your discussions and I can't find a solution.

  19. 1ec5 commented on Jul 26, 2019

    @1ec5
    ContributorAuthor

    In mapbox-ios: I wonder if modifying styleURL to preserve layers and sources is supported?
    Is it implemented as a plug-in?
    I've looked at all your discussions and I can't find a solution.

    No, there is no general solution to the problem at the moment. Your options are:

    • Manually redo any runtime styling changes.
    • Avoid switching styles and instead manipulate the current style to look like a different style.
    • Use annotations for pins and overlays. However, note that the plan in Reimplement MGLAnnotation atop MGLSource and MGLStyleLayer #6181 is to reimplement annotations as style layers. Unfortunately, it’s likely that that’ll happen before this issue is fixed, so annotations will no longer persist across styles and you’ll need to use one of the other options.

    /cc @chloekraw

  20. added
    archived
    on May 20, 2020
  21. stale commented on May 22, 2020

    @stale

    This issue has been automatically detected as stale because it has not had recent activity and will be archived. Thank you for your contributions.

  22. removed
    archived
    on May 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    annotationsfeaturegl-iosiOSmacOSruntime styling

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions