Managing feature visibility for Mapbox
Overview
From Mapbox v3 (Web) and v11 (Mobile), it's now possible to control what features (layers) should be shown or hidden at runtime. We are introducing three new methods to enhance the control and visibility of features on your Mapbox map. With these methods, you can easily manipulate the visibility of specific features, enhancing the user experience and providing more control over map customization. As an example, you could easily change between a 2D and a 3D map at runtime, without reloading the map data.
Methods
hideFeatures(features: string[]): void
hideFeatures(features: string[]): void
This method allows you to hide specific Mapbox features by passing an array of feature identifiers. Once hidden, the features will no longer be visible on the map. This can be useful for scenarios where certain map elements need to be temporarily removed from view.
Parameters
features
(Array): An array ofFeatureType
objects representing the features to be hidden.
Switching between 2D and 3D features example:
Example when calling hide3DFeatures()
function:
Example when calling hide2DFeatures()
function:
getHiddenFeatures(): string[]
getHiddenFeatures(): string[]
This method retrieves the currently hidden features on the map. It returns an array of feature identifiers representing the features that are currently not visible.
Returns
string[]
: An array of strings representing the identifiers of the currently hidden features.
Example
FeatureType(): string[]
FeatureType(): string[]
This method retrieves all the available feature identifiers that can have their visibility set to "none". It returns an array of these feature identifiers, providing developers with an overview of the features that can be hidden.
Returns
string[]
: An array of strings representing the identifiers of the features that can be hidden.
Example
NOTE: MapboxFeatures()
method is deprecated. It still works as expected but will be removed within next major release.
Conclusion
You now have powerful tools to control the visibility of features on your maps. By utilizing the hideFeatures
, FeatureType
, and getHiddenFeatures
methods, you can enhance user experience and customize map displays according to your needs.
See example of the implementation here.
Last updated