MapSnippets logo MapSnippets logo

Style Expressions with MapTiler SDK

MapTiler SDK

Style earthquake circles in MapTiler SDK with interpolate expressions on the mag property that drive the circle-radius and circle-color paint properties.

progress_activity Loading map...
maptilersdk.config.apiKey = 'YOUR_MAPTILER_KEY';
const map = new maptilersdk.Map({
  container: 'map',
  style: maptilersdk.MapStyle.STREETS,
  center: [-98, 39],
  zoom: 3
});
map.on('load', () => {
  map.addSource('quakes', {
    type: 'geojson',
    data: 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson'
  });
  map.addLayer({
    id: 'quake-circles', type: 'circle', source: 'quakes',
    paint: {
      'circle-radius': ['interpolate', ['linear'], ['get', 'mag'], 1, 3, 5, 15, 8, 30],
      'circle-color': ['interpolate', ['linear'], ['get', 'mag'],
        1, '#e8d5f5', 3, '#ce93d8', 5, '#9C27B0', 7, '#4a148c'],
      'circle-opacity': 0.7
    }
  });
});

How to style features by data with expressions in MapTiler SDK

Expressions let you style features based on their data properties. This example sizes and colors earthquake circles by magnitude using interpolate expressions.

The circle-radius expression scales from 3px at magnitude 1 to 30px at magnitude 8. The circle-color expression maps the same property to a purple gradient. Both use linear interpolation for smooth transitions.

Expressions evaluate per feature, so each earthquake gets its own radius and color based on its mag property.

Key APIs

API Description Docs
interpolate Continuous mapping between input/output pairs docs.maptiler.com/sdk-js/api/
step Discrete mapping at threshold values docs.maptiler.com/sdk-js/api/
match Categorical mapping for string/number values docs.maptiler.com/sdk-js/api/

FAQ

What is the difference between interpolate and step?

interpolate produces smooth transitions between stops. step produces flat segments that jump at each threshold, like a histogram.

Can I use expressions in layout properties too?

Yes. For example, text-size and icon-size in symbol layers accept expressions.

Common mistakes

  • Mixing up interpolate and step. Use interpolate for gradients and step for discrete buckets
  • Forgetting the interpolation type (['linear']), which is required as the second argument
  • Using ['get', 'property'] on a property that does not exist. The expression returns null and the feature may not render

Build maps using MapTiler SDK with AI

Generate code like this style expressions with maptiler sdk example using the MapTiler SDK skill in your IDE.