MapSnippets logo MapSnippets logo

Choropleth Map with OpenLayers

OpenLayers

Color GeoJSON polygons by value in OpenLayers with a style function on an ol.layer.Vector that returns an ol.style.Style with a data-driven ol.style.Fill.

progress_activity Loading map...
function getColor(value) {
  return value > 80 ? '#b5179e' : value > 60 ? '#e63946' : value > 40 ? '#f4a261' : value > 20 ? '#e9c46a' : '#2a9d8f';
}
var geojson = {
  type: 'FeatureCollection',
  features: [
    { type: 'Feature', properties: { name: 'Zone A', value: 85 }, geometry: { type: 'Polygon', coordinates: [[[2.30, 48.87], [2.34, 48.87], [2.34, 48.85], [2.30, 48.85], [2.30, 48.87]]] } },
    { type: 'Feature', properties: { name: 'Zone B', value: 45 }, geometry: { type: 'Polygon', coordinates: [[[2.34, 48.87], [2.38, 48.87], [2.38, 48.85], [2.34, 48.85], [2.34, 48.87]]] } },
    { type: 'Feature', properties: { name: 'Zone C', value: 15 }, geometry: { type: 'Polygon', coordinates: [[[2.30, 48.85], [2.34, 48.85], [2.34, 48.83], [2.30, 48.83], [2.30, 48.85]]] } }
  ]
};
var vectorLayer = new ol.layer.Vector({
  source: new ol.source.Vector({
    features: new ol.format.GeoJSON().readFeatures(geojson, { featureProjection: 'EPSG:3857' })
  }),
  style: function(feature) {
    return new ol.style.Style({
      fill: new ol.style.Fill({ color: getColor(feature.get('value')) + '99' }),
      stroke: new ol.style.Stroke({ color: '#333', width: 1 })
    });
  }
});
var map = new ol.Map({
  target: 'map',
  layers: [
    new ol.layer.Tile({ source: new ol.source.XYZ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', attributions: '&copy; <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) }),
    vectorLayer
  ],
  view: new ol.View({ center: ol.proj.fromLonLat([2.34, 48.855]), zoom: 14 })
});

How to color polygons by data value in OpenLayers

A choropleth map colors regions based on data values. In OpenLayers, this is achieved with a style function that reads each feature’s properties and returns a matching style.

The getColor() helper maps numeric values to color thresholds. The style function is called for every feature during rendering, so colors update automatically if feature properties change. GeoJSON data is parsed with ol.format.GeoJSON using featureProjection: 'EPSG:3857' to convert coordinates.

Key APIs

API Description Docs
ol.format.GeoJSON Parse GeoJSON into OpenLayers features openlayers.org
Style function Dynamic styling per feature openlayers.org
feature.get() Read feature properties by key openlayers.org

FAQ

How do I load GeoJSON from an external URL?

Use new ol.source.Vector({ url: 'data.geojson', format: new ol.format.GeoJSON() }). The source fetches and parses the file automatically.

How do I add a legend?

Create an HTML element with color swatches matching your threshold values and position it as a map control or overlay.

Common mistakes

  • Forgetting featureProjection: 'EPSG:3857' when parsing GeoJSON, which places features near null island
  • Using fully opaque fills that obscure the basemap. Append an alpha value ('99' hex) or use rgba() instead.
  • Not closing polygon rings in the GeoJSON data

Build maps using OpenLayers with AI

Generate code like this choropleth map with openlayers example using the OpenLayers skill in your IDE.