You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
functionswitchLayer(layer){map.setStyle('mapbox://styles/mapbox/'+layer+'-v8');map.on('style.load',function(){addLayerHeat(map);//add custom layer and source to the new map styleaddLayerLinestring(map);//add custom layer and source to the new map style});}
An example of what the "addLayerLinestring(map)" function does:
It would be convenient if there were a method to only replace the "base map" styles and layers.
Thoughts?
Reacted by Danny Delott, Guilherme Prezzi, Saharsh Bhatia, vanhumbeecka, dafnarosenblum, Sean Kovacs, Adlen Afane, Matthew Geiger, Alexander Kennedy, Avi Nerenberg and 2 more
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.
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');
}
}
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');
}
}
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?
Activity
ryanbaumann commentedon Mar 15, 2016
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.
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
An example of what the "addLayerLinestring(map)" function does:
It would be convenient if there were a method to only replace the "base map" styles and layers.
Thoughts?
Iam-jegan commentedon Mar 15, 2016
Absolutely it will be convenient if there is an option to just change the basemap styles and layers.
Thanks,
Jegan
lucaswoj commentedon Mar 15, 2016
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.
emakarov commentedon Dec 26, 2016
definitely, in a dynamic applications changing styles of base map directly is required
emakarov commentedon Dec 26, 2016
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" });emakarov commentedon Dec 26, 2016
I found the solution by manual switching layers of base map (now works for lightv9 scheme)
so,
`
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');
}
`
emakarov commentedon Dec 26, 2016
it will be good if we could have a standard method of map in API to get all layers of "base map"
emakarov commentedon Dec 26, 2016
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');
}
}
aabhishekbindal commentedon Dec 1, 2017
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
emakarov commentedon Dec 1, 2017
@aabhishekbindal see my solution described above. it works and easy
I did it for switching between basic map and satellite map