Why Use Expressions?
Expressions enable:- Data-driven styling - Style features based on their properties
- Zoom-based styling - Change appearance at different zoom levels
- Dynamic calculations - Compute values on the fly
- Conditional logic - Apply styles based on conditions
- Feature state - Style based on interactive state
Expression Syntax
Expressions use a JSON array format:Expression Types
Getting Feature Properties
get
Retrieve a feature property value:has
Check if a property exists:Type Operators
to-number, to-string, to-boolean
Convert between types:typeof
Get the type of a value:Conditional Expressions
match
Category-based styling (like a switch statement):case
Conditional styling with boolean conditions:Comparison Operators
Logical Operators
Interpolation
Smooth transitions between values.Linear Interpolation
Exponential Interpolation
Faster growth for a more dramatic effect:Cubic Bezier Interpolation
Custom easing curves:Property-based Interpolation
Step Functions
Discrete value changes (no interpolation):Mathematical Operations
String Operations
concat
Combine strings:upcase, downcase
Zoom-based Expressions
Access the current zoom level:Feature State Expressions
Style based on dynamic feature state:Heatmap Color Expression
Heatmap layers require a color expression:Line Gradient Expression
Line gradients require
lineMetrics: true on the ShapeSource.Practical Examples
Earthquake Visualization
Population Density
Route Highlighting
Zoom-dependent Icons
Complete Example
Best Practices
- Use appropriate interpolation: Linear for smooth transitions, exponential for dramatic effects, step for discrete categories
- Combine expressions: Nest expressions for complex logic
- Consider performance: Complex expressions can impact performance - test on devices
- Use feature state: For interactive styling without updating GeoJSON
- Validate types: Use type conversion operators to ensure correct data types
Related
- Styling - Layer style properties
- Layers - Layer types and configuration
- Mapbox Expression Reference - Complete specification