Skip to content

SetStyle on map with multiple layers #2267

Closed

Description

@Iam-jegan

mapbox-gl-js version:

Steps to Trigger Behavior

  1. Initially load map with several layers(each layer has circles of different radius) and with a basic style
  2. After it has been loaded, change the style of the map to anyother style using setStyle method.
  3. map changes it style and layers drawn previouly are gone.

Expected Behavior

I need to retain the layers and just to change the map style

Actual Behavior

layers are no more adn i couldn't able to get it.

Activity

  1. ryanbaumann commented on Mar 15, 2016

    @ryanbaumann
    Contributor

    When you change the map style using setStyle(), are you replacing it with a Mapbox:// vector tile reference?

    SetStyle() replaces all of the layers and styles on a map with a new set of layers and styles - in the case above, the entire map will be set to "streets-v8" and all existing layers will be replaced.

    There are two solutions I've used.

    1. Prior to SetStyle(), call map.getStyle() which returns the current map and layer information all in one object. Use the getStyle result to create a new map instance, or manipulate the style to fit your needs.

    2)First use map.getLayer() and map.getSource() to parse out the layers and sources currently defined in your map. Then use map.SetStyle(). Finally, add the sources and layers back to your map. Example below, where

    function switchLayer(layer) {
        map.setStyle('mapbox://styles/mapbox/' + layer + '-v8');
        map.on('style.load', function() {
            addLayerHeat(map);        //add custom layer and source to the new map style
            addLayerLinestring(map);   //add custom layer and source to the new map style
        });
    }

    An example of what the "addLayerLinestring(map)" function does:

    function addLayerLinestring(mapid, lineLayers, linestring_src) {
            linestring_src = new mapboxgl.GeoJSONSource({
                data: heatline_url
            });
            mapid.addSource('linestring', linestring_src);
            mapid.batch(function(batch) {
                for (var p = 0; p < lineLayers.length; p++) {
                    batch.addLayer(lineLayers[p]);
                }
            });
    };

    It would be convenient if there were a method to only replace the "base map" styles and layers.
    Thoughts?

  2. Iam-jegan commented on Mar 15, 2016

    @Iam-jegan
    Author

    Absolutely it will be convenient if there is an option to just change the basemap styles and layers.

    Thanks,
    Jegan

  3. lucaswoj commented on Mar 15, 2016

    @lucaswoj
    Contributor

    Unlike Leaftlet, mapbox-gl-js doesn't have a concept of "basemap" vs "other layers." All layers are part of the same entity, the style. You will have to find another way to achieve your desired functionality.

    I encourage you to ask about that on StackOverflow rather than this issue tracker. This issue tracker is reserved for feature requests and bug reports only.

  4. emakarov commented on Dec 26, 2016

    @emakarov

    definitely, in a dynamic applications changing styles of base map directly is required

  5. emakarov commented on Dec 26, 2016

    @emakarov

    meanwhile I'm experimenting with manual satellite layer manipulation. I think manual layers manipulation can be key to solution, i.e.
    map.addSource("mapbox-satellite", { "type": "raster", "url": "mapbox://mapbox.satellite", "tileSize": 256 }); map.addLayer({ "type": "raster", "id": 'satellite-map', "source": "mapbox-satellite" });

  6. emakarov commented on Dec 26, 2016

    @emakarov

    I found the solution by manual switching layers of base map (now works for lightv9 scheme)
    so,

    1. I init the map with light-v9
    2. I take raw data of light v9 from here https://raw.githubusercontent.com/mapbox/mapbox-gl-js/8f0475e66822a79d8a1ddfc8031c4733373c7d07/debug/light-v9.js
    3. Then, idea is manually turning on and off all layers from light-v9:
      `
      map.addSource("mapbox-satellite", {
      "type": "raster",
      "url": "mapbox://mapbox.satellite",
      "tileSize": 256
      });

    map.base_layers = lightv9.layers;

    map.hide_base_layers = function() {
    for (var v in map.base_layers) {
    map.setLayoutProperty(map.base_layers[v]['id'], 'visibility', 'none');
    }
    }
    map.show_base_layers = function() {
    for (var v in map.base_layers) {
    map.setLayoutProperty(map.base_layers[v]['id'], 'visibility', 'visible');
    }
    }

    map.turnOnSatellite = function() {
    map.addLayer({
    "type": "raster",
    "id": 'satellite-map',
    "source": "mapbox-satellite",
    "opacity": 0.6
    }, 'lowest_device_layer');
    map.hide_base_layers();
    }

    map.turnOffSatellite = function() {
    map.removeLayer('satellite-map');
    }
    `

  7. emakarov commented on Dec 26, 2016

    @emakarov

    it will be good if we could have a standard method of map in API to get all layers of "base map"

  8. emakarov commented on Dec 26, 2016

    @emakarov

    note -
    I managed to get "base layers" on load, like this (with jquery):
    (before loading all other layers):
    map.on('load', function () {
    map.base_layers = $.extend(true, {}, that.map.style._layers);
    }

    then you can hide base layers like:

    map.hide_base_layers = function() {
    for (var v in map.base_layers) {
    map.setLayoutProperty(v, 'visibility', 'none');
    }
    }

  9. aabhishekbindal commented on Dec 1, 2017

    @aabhishekbindal

    I saw that this has been an issue for many people now. Has @mapbox team found a solution to this? or a direct way on how I can change the base map keeping the other layers intact?

    Regards,
    Abhishek

  10. emakarov commented on Dec 1, 2017

    @emakarov

    @aabhishekbindal see my solution described above. it works and easy
    I did it for switching between basic map and satellite map

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions