Skip to content

Design Proposal: Global map state #886

@hiddewie

Description

@hiddewie

Design Proposal: Global map state

Motivation

Split off from #516 (comment), and originally posted / requested in maplibre/maplibre-gl-js#4964.

The goal is to store some global map state on the Map object, which can be used in the style. This makes it easier to let the user configure the map for certain preferences, which can then dynamically be used in the style to modify the appearance of the map.

My concrete use case is supporting a map theme, where the user selects a theme and this impacts the look and feel of the web application, in particular the features on the map.

Proposed Change

Introduce a function map-state which takes a single string argument which is the key.

The return value is the value in the global map state, if present, otherwise null.

Examples:

paint: {
  'text-color': ['map-state', 'color'],
}

Handling unset key in the map state

paint: {
  'text-color': ['coalesce', ['map-state', 'color'], 'red']
}

The map state will function as a key-value dictionary, so the values in the map state contains string keys and string, numeric or boolean values. We could choose to support e.g. arrays as values as well.

The map-state function should have the same semantics as feature-state which uses data set on a feature.

API Modifications

The expression map-state will be supported.

Migration Plan and Compatibility

No migration needed, this is new functionality.

The feature should not be complex to implement in the specification and client libraries because:

  • the function feature-state already exists, and this function is similar although it takes the state from a different place
  • the map state is a new feature, so this will not conflict with existing features.

Probably the biggest complexity is managing the global map state changing its value, and recomputing the style for the impacted features on the map.

Rejected Alternatives

  • Support global map state maplibre-gl-js#4964 (comment): Changing the style dynamically during loading of the map: this makes the map style highly coupled with the client using the style. In particular, the client application has to walk every style expression, and match the expression against the replacements with the configured global values from the user configuration.
  • Support global map state maplibre-gl-js#4964 (comment): using let expressions and dynamically transforming the style. This still couples the client with the map style, and still requires walking the expressions in the map style to dynamically replace the let values with the configured values.
  • Proposal: Support registering external functions to be used in expressions in the style #516: Design proposal to add arbitrary functions to the map. That proposal would replace this proposal, but the scope is much bigger and more complex to implement than this feature.
  • Proposal: Support registering external functions to be used in expressions in the style #516 (comment): Generate a style per value of the user configuration. This is possible for global map state with a finite set of values. Once the values become non-enumerable (like numbers between 0 and 1), or many properties which can all be combined, the number of JSON styles to generate explodes. It is possible to create a server to dynamically create a style on request, taking the user parameters into account, but this requires a server creating the styles on demand, instead of putting static files in a web file server.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions