-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
map.getstyle() can't get custom layer #9001
Comments
Custom layers are explicitly excluded from mapbox-gl-js/src/style/style.js Lines 322 to 331 in 254674e
getStyle is used for serializing a style to a JSON data structure, and custom layers contain functions that are cannot be represented in JSON.
To obtain an individual custom layer, use |
Can we make this a feature request then? We have a scenario where we're adding n custom layers, and need to track what has been added without knowing their ids ahead of time. I thought prefixing and filtering |
Seems it just searches the _layers object? mapbox-gl-js/src/style/style.js Lines 750 to 758 in 254674e
Something like this could work: /**
* Return the style layer object with the given `id`.
*
* @param {string} id - id of the desired layer
* @returns {?Object} a layer, if one with the given `id` exists
*/
getLayer(id: string): Object {
return this._layers[id];
}
/**
* Return style layers whose `id` matches a search string
*
* @param {string} search - search string to filter layer ids
* @returns {?Object[]} an array of layers, if any that include the given `search` in their `id` exists
*/
getLayers(search: string): Object[] {
return Object.entries(this._layers)
.filter(([id]) => id.includes(search))
.map(([,layer]) => layer)
}
// or if you want to keep it OOP for referential equality
getLayers(search: string): Object[] {
const results = [];
const ids = Object.entries(this._layers)
.filter(([id]) => id.includes(search))
.foreach(([id]) => results.push(this._layers[id]))
return results;
} Would be happy to make a PR if you'll accept it! |
@AJIKO You can access it currently with |
@mayteio same explanation as above: the idea of |
OK great, thanks for the explanation. Unfortunately we have a situation with deck.gl where we have an array of layers that we add to Mapbox via @deck.gl/mapbox. If an item is removed from that array, we have no way of knowing what it’s ID was. Thus we prefix all layers with “custom-“, find them in mapbox, find the difference (I.e those who are in mapbox and not in the deck.gl array) and then remove them. The only way for us is to use map._layers as these custom layers are (rightly) not in the style JSON. |
So how do I get a list of all the layers I added?
|
When you call |
map.getstyle() method can get many layer in style but no custom layers, like deck.gl layer. how can I get all the layers include deck.gl layer?
The text was updated successfully, but these errors were encountered: