How to create a choropleth Map from GeoJSON using OpenLayers

This tutorial will walk you through the process of adding a styled GeoJSON overlay to your map, displaying a popup on click, and creating a map legend. To demonstrate, we will be using GeoJSON formatted data from EUROSTAT that includes countries with attributes. Download the sample data for average age at first marriage.

  1. Copy the following code, paste it into your favorite text editor, and save it as a .html file.

Check out the step-by-step tutorial How to use OpenLayers

  1. Install the npm package.

  2. Include the CSS file.

    If you have a bundler that can handle CSS, you can import the CSS or include it with a <link> in the head of the document via the CDN

  3. Include the following code in your JavaScript file (Example: main.js).

  1. Replace YOUR_MAPTILER_API_KEY_HERE with your actual MapTiler API key.

  2. The next is up to you. You can center your map wherever you desire (modifying the starting position) and set an appropriate zoom level (modifying the starting zoom) to match your users’ needs. Additionally, you can change the map’s look (by updating the source URL); choose from a range of visually appealing map styles from our extensive MapTiler standard maps, or create your own to truly differentiate your application.

  1. Add the GeoJSON layer. The following snippet creates GeoJSON layer hosted on MapTiler (check out the How to Upload GeoJSON to MapTiler tutorial). The GeoJSON used in the example contains Marriage indicators data from EUROSTAT. The dataset was filtered to just countries with some value of mean age of women at first marriage in 2019. Download the sample data for average age at first marriage

  2. Create the functions to get the style based on the age attribute.

  3. Create a choropleth map based on the age attribute. Change the style property of the layer.

  4. Create the popup html element.

  5. Create the style for the popup

  6. Create the overlay to anchor the popup

  7. Add the overlay to the map

  8. Create a function to display a popup when clicking on the geojson layer and show the information of the age attribute

  9. Create a map legend.

  10. Create the style for the map legend.

  1. Import the new modules needed to add the new functionality.

    
     import Map from 'ol/Map.js';
     import View from 'ol/View.js';
     import TileLayer from 'ol/layer/Tile.js';
     import VectorLayer from 'ol/layer/Vector.js';
     import VectorSource from 'ol/source/Vector.js';
     import TileJSON from 'ol/source/TileJSON.js';
     import Style from 'ol/style/Style.js';
     import Fill from 'ol/style/Fill.js';
     import Stroke from 'ol/style/Stroke.js';
     import Attribution from 'ol/control/Attribution.js';
     import GeoJSON from 'ol/format/GeoJSON.js';
     import Overlay from 'ol/Overlay.js';
     import { defaults as defaultControls } from 'ol/control/defaults.js';
     import { fromLonLat } from 'ol/proj.js';
     
  2. Add the GeoJSON layer. The following snippet creates GeoJSON layer hosted on MapTiler (check out the How to Upload GeoJSON to MapTiler tutorial). The GeoJSON used in the example contains Marriage indicators data from EUROSTAT. The dataset was filtered to just countries with some value of mean age of women at first marriage in 2019. Download the sample data for average age at first marriage

  3. Create the functions to get the style based on the age attribute.

  4. Create a choropleth map based on the age attribute. Change the style property of the layer.

  5. Create the popup html element. Add the element inside your map container.

  6. Create the style for the popup. Add this lines into the CSS file

  7. Create the overlay to anchor the popup

  8. Add the overlay to the map

  9. Create a function to display a popup when clicking on the geojson layer and show the information of the age attribute

  10. Create a map legend. Update your html file add this lines after the map container.

  11. Create the style for the map legend. Add this code in your CSS file.

Learn more

Get more details about this tutorial on Zoomable Choropleth Map from GeoJSON with OpenLayers or check out the following How-tos related to choropleth maps

How to style a choropleth map in Edit Tool

Prepare GeoJSON with attributes for choropleth map and upload GeoJSON to MapTiler