Skip to content

Commit f7e35a9

Browse files
author
Colleen McGinnis
authored
[docsprint] Add detail to docs on setFeatureState, removeFeatureState, getFeatureState (#9559)
* add detail to docs on setFeatureState, removeFeatureState, getFeatureState * address asheemmamoowala's feedback * remove trailing spaces
1 parent d821161 commit f7e35a9

File tree

1 file changed

+92
-27
lines changed

1 file changed

+92
-27
lines changed

src/ui/map.js

Lines changed: 92 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,23 +1914,42 @@ class Map extends Camera {
19141914

19151915
// eslint-disable-next-line jsdoc/require-returns
19161916
/**
1917-
* Sets the state of a feature. The `state` object is merged in with the existing state of the feature.
1918-
* Features are identified by their `id` attribute, which must be an integer or a string that can be
1919-
* cast to an integer.
1917+
* Sets the `state` of a feature.
1918+
* A feature's `state` is a set of user-defined key-value pairs that are assigned to a feature at runtime.
1919+
* When using this method, the `state` object is merged with any existing key-value pairs in the feature's state.
1920+
* Features are identified by their `feature.id` attribute, which can be any number or string.
1921+
*
1922+
* This method can only be used with sources that have a `feature.id` attribute. The `feature.id` attribute can be defined in three ways:
1923+
* - For vector or GeoJSON sources, including an `id` attribute in the original data file.
1924+
* - For vector or GeoJSON sources, using the [`promoteId`](https://docs.mapbox.com/mapbox-gl-js/style-spec/sources/#vector-promoteId) option at the time the source is defined.
1925+
* - For GeoJSON sources, using the [`generateId`](https://docs.mapbox.com/mapbox-gl-js/style-spec/sources/#geojson-generateId) option to auto-assign an `id` based on the feature's index in the source data. If you change feature data using `map.getSource('some id').setData(..)`, you may need to re-apply state taking into account updated `id` values.
1926+
*
1927+
* _Note: You can use the [`feature-state` expression](https://docs.mapbox.com/mapbox-gl-js/style-spec/expressions/#feature-state) to access the values in a feature's state object for the purposes of styling._
19201928
*
19211929
* @param {Object} feature Feature identifier. Feature objects returned from
19221930
* {@link Map#queryRenderedFeatures} or event handlers can be used as feature identifiers.
19231931
* @param {string | number} feature.id Unique id of the feature.
1924-
* @param {string} feature.source The Id of the vector source or GeoJSON source for the feature.
1925-
* @param {string} [feature.sourceLayer] (optional) *For vector tile sources, the sourceLayer is
1926-
* required.*
1932+
* @param {string} feature.source The id of the vector or GeoJSON source for the feature.
1933+
* @param {string} [feature.sourceLayer] (optional) *For vector tile sources, `sourceLayer` is required.*
19271934
* @param {Object} state A set of key-value pairs. The values should be valid JSON types.
19281935
*
1929-
* This method requires the `feature.id` attribute on data sets. For GeoJSON sources without
1930-
* feature ids, set the `generateId` option in the `GeoJSONSourceSpecification` to auto-assign them. This
1931-
* option assigns ids based on a feature's index in the source data. If you change feature data using
1932-
* `map.getSource('some id').setData(..)`, you may need to re-apply state taking into account updated `id` values.
1936+
* @example
1937+
* // When the mouse moves over the `my-layer` layer, update
1938+
* // the feature state for the feature under the mouse
1939+
* map.on('mousemove', 'my-layer', (e) => {
1940+
* if (e.features.length > 0) {
1941+
* map.setFeatureState({
1942+
* source: 'my-source',
1943+
* sourceLayer: 'my-source-layer',
1944+
* id: e.features[0].id,
1945+
* }, {
1946+
* hover: true
1947+
* });
1948+
* }
1949+
* });
1950+
*
19331951
* @see [Create a hover effect](https://docs.mapbox.com/mapbox-gl-js/example/hover-styles/)
1952+
* @see Tutorial: [Create interactive hover effects with Mapbox GL JS](https://docs.mapbox.com/help/tutorials/create-interactive-hover-effects-with-mapbox-gl-js/)
19341953
*/
19351954
setFeatureState(feature: { source: string; sourceLayer?: string; id: string | number; }, state: Object) {
19361955
this.style.setFeatureState(feature, state);
@@ -1939,38 +1958,84 @@ class Map extends Camera {
19391958

19401959
// eslint-disable-next-line jsdoc/require-returns
19411960
/**
1942-
* Removes feature state, setting it back to the default behavior. If only
1943-
* source is specified, removes all states of that source. If
1944-
* target.id is also specified, removes all keys for that feature's state.
1945-
* If key is also specified, removes that key from that feature's state.
1946-
* Features are identified by their `id` attribute, which must be an integer or a string that can be
1947-
* cast to an integer.
1948-
* @param {Object} target Identifier of where to set state: can be a source, a feature, or a specific key of feature.
1961+
* Removes the `state` of a feature, setting it back to the default behavior.
1962+
* If only a `target.source` is specified, it will remove the state for all features from that source.
1963+
* If `target.id` is also specified, it will remove all keys for that feature's state.
1964+
* If `key` is also specified, it removes only that key from that feature's state.
1965+
* Features are identified by their `feature.id` attribute, which can be any number or string.
1966+
*
1967+
* @param {Object} target Identifier of where to remove state. It can be a source, a feature, or a specific key of feature.
19491968
* Feature objects returned from {@link Map#queryRenderedFeatures} or event handlers can be used as feature identifiers.
19501969
* @param {string | number} target.id (optional) Unique id of the feature. Optional if key is not specified.
1951-
* @param {string} target.source The Id of the vector source or GeoJSON source for the feature.
1952-
* @param {string} [target.sourceLayer] (optional) *For vector tile sources, the sourceLayer is
1953-
* required.*
1970+
* @param {string} target.source The id of the vector or GeoJSON source for the feature.
1971+
* @param {string} [target.sourceLayer] (optional) *For vector tile sources, `sourceLayer` is required.*
19541972
* @param {string} key (optional) The key in the feature state to reset.
1973+
*
1974+
* @example
1975+
* // Reset the entire state object for all features
1976+
* // in the `my-source` source
1977+
* map.removeFeatureState({
1978+
* source: 'my-source'
1979+
* });
1980+
*
1981+
* @example
1982+
* // When the mouse leaves the `my-layer` layer,
1983+
* // reset the entire state object for the
1984+
* // feature under the mouse
1985+
* map.on('mouseleave', 'my-layer', (e) => {
1986+
* map.removeFeatureState({
1987+
* source: 'my-source',
1988+
* sourceLayer: 'my-source-layer',
1989+
* id: e.features[0].id
1990+
* });
1991+
* });
1992+
*
1993+
* @example
1994+
* // When the mouse leaves the `my-layer` layer,
1995+
* // reset only the `hover` key-value pair in the
1996+
* // state for the feature under the mouse
1997+
* map.on('mouseleave', 'my-layer', (e) => {
1998+
* map.removeFeatureState({
1999+
* source: 'my-source',
2000+
* sourceLayer: 'my-source-layer',
2001+
* id: e.features[0].id
2002+
* }, 'hover');
2003+
* });
2004+
*
19552005
*/
19562006
removeFeatureState(target: { source: string; sourceLayer?: string; id?: string | number; }, key?: string) {
19572007
this.style.removeFeatureState(target, key);
19582008
return this._update();
19592009
}
19602010

19612011
/**
1962-
* Gets the state of a feature.
1963-
* Features are identified by their `id` attribute, which must be an integer or a string that can be
1964-
* cast to an integer.
2012+
* Gets the `state` of a feature.
2013+
* A feature's `state` is a set of user-defined key-value pairs that are assigned to a feature at runtime.
2014+
* Features are identified by their `feature.id` attribute, which can be any number or string.
2015+
*
2016+
* _Note: To access the values in a feature's state object for the purposes of styling the feature, use the [`feature-state` expression](https://docs.mapbox.com/mapbox-gl-js/style-spec/expressions/#feature-state)._
19652017
*
19662018
* @param {Object} feature Feature identifier. Feature objects returned from
19672019
* {@link Map#queryRenderedFeatures} or event handlers can be used as feature identifiers.
19682020
* @param {string | number} feature.id Unique id of the feature.
1969-
* @param {string} feature.source The Id of the vector source or GeoJSON source for the feature.
1970-
* @param {string} [feature.sourceLayer] (optional) *For vector tile sources, the sourceLayer is
1971-
* required.*
2021+
* @param {string} feature.source The id of the vector or GeoJSON source for the feature.
2022+
* @param {string} [feature.sourceLayer] (optional) *For vector tile sources, `sourceLayer` is required.*
2023+
*
2024+
* @returns {Object} The state of the feature: a set of key-value pairs that was assigned to the feature at runtime.
2025+
*
2026+
* @example
2027+
* // When the mouse moves over the `my-layer` layer,
2028+
* // get the feature state for the feature under the mouse
2029+
* map.on('mousemove', 'my-layer', (e) => {
2030+
* if (e.features.length > 0) {
2031+
* map.getFeatureState({
2032+
* source: 'my-source',
2033+
* sourceLayer: 'my-source-layer'
2034+
* id: e.features[0].id
2035+
* });
2036+
* }
2037+
* });
19722038
*
1973-
* @returns {Object} The state of the feature.
19742039
*/
19752040
getFeatureState(feature: { source: string; sourceLayer?: string; id: string | number; }): any {
19762041
return this.style.getFeatureState(feature);

0 commit comments

Comments
 (0)