How to create a choropleth Map from GeoJSON

This tutorial demonstrates the process of generating a Choropleth map through the application of styling to a GeoJSON overlay layer on the map. Additionally, it explains how to display a popup when clicked and create a map legend. As an illustrative example, we will be using countries with attributes sourced from EUROSTAT in GeoJSON format. You can download the Age of the first marriage sample data.

NPM module setup

  1. Install the npm package.

    
     npm install --save @maptiler/sdk
    
  2. Include the CSS file.

    If you have a bundler that can handle CSS, you can import the CSS

    
     import "@maptiler/sdk/dist/maptiler-sdk.css";
    

    or include it with a <link /> in the head of the document via the CDN

    
     <link href='https://cdn.maptiler.com/maptiler-sdk-js/v4.1.0/maptiler-sdk.css' rel='stylesheet' />
    
  3. Include the following code in your JavaScript file (Example: app.js).

    
     import * as maptilersdk from '@maptiler/sdk';
    
     maptilersdk.config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
     const map = new maptilersdk.Map({
       container: 'map', // container's id or the HTML element to render the map
       style: maptilersdk.MapStyle.DATAVIZ.DARK,
          
       center: [13.39, 52.51], // starting position [lng, lat]
       zoom: 2, // starting zoom
          
     });
    
  4. Replace YOUR_MAPTILER_API_KEY_HERE with your own API key. Make sure to secure the key before you publish it.

  5. 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.

  6. Add event handler for map load event. You will add code to create a GeoJSON source and a vector layer in this handler.

    
     map.on('load', async function() {
    
     });
    
  7. Create GeoJSON source. The following snippet creates GeoJSON source hosted on MapTiler (check out the How to Upload GeoJSON to MapTiler tutorial). Publish the dataset and copy the link to the GeoJSON. The GeoJSON used in the example contains data from EUROSTAT Marriage indicators dataset filtrated to just countries with some value of mean age of women at first marriage in 2019. Download the Age of the first marriage sample data.

    
     map.on('load', async function() {
       const result = await maptilersdk.data.get('YOUR_MAPTILER_DATASET_ID_HERE');
       map.addSource('age', {
         'type': 'geojson',
         'data': result
       });
     });
    
  8. Get the ID of the first symbol layer. We want to include GeoJSON below the map labels. That means we need to know the ID of the first symbol layer so we can include the GeoJSON layer before this layer.

    
     // Find the ID of the first symbol layer in the map style
     const layers = map.getStyle().layers;
     const firstSymbolId = layers.find(layer => layer.type === 'symbol').id;
    
  9. Add the GeoJSON layer. We need to include firsSymbolId on the map.addLayer function to display the GeoJSON under the map’s labels.

    
     map.addLayer(
       {
         'id': 'IDage',
         'source': 'age',
         'type': 'fill',
         'paint': {
             'fill-color': '#6B7C93',
             'fill-opacity': 1,
             'fill-outline-color': '#000'
         }
       },
       firstSymbolId   
     );
    
  10. Create a choropleth map based on the age attribute. Change the fill-color property of the layer.

    
     map.addLayer(
       {
         'id': 'IDage',
         'source': 'age',
         'type': 'fill',
         'paint': {
             'fill-color': [
               'interpolate',
               ['linear'],
               ['get', 'age'],
               23.0,
               '#fff5eb',
               24.0,
               '#fee6ce',
               25.0,
               '#fdd0a2',
               26.0,
               '#fdae6b',
               27.0,
               '#fd8d3c',
               28.0,
               '#f16913',
               29.0,
               '#d94801',
               30.0,
               '#8c2d04'
             ],
             'fill-opacity': 1,
             'fill-outline-color': '#000'
         }
       },
       firstSymbolId   
     );
    
  11. Display a popup when clicking on the geojson layer and show the information of the age attribute.

    
     map.on('click', 'IDage', function (e) {
       new maptilersdk.Popup()
         .setLngLat(e.lngLat)
         .setHTML(`<h3>Average age of </br> women at first marriage </br>in 2019</h3><p>${e.features[0].properties.age}</p>`)
         .addTo(map);
     });
    
  12. To make our map more user-friendly, we will change the cursor when hovering over a geometry in the geojson layer to indicate to the user that they can click on it.

    
     // Change the cursor to a pointer when the mouse is over the layer.
     map.on('mouseenter', 'IDage', function () {
         map.getCanvas().style.cursor = 'pointer';
     });
    
     // Change it back to a pointer when it leaves.
     map.on('mouseleave', 'IDage', function () {
         map.getCanvas().style.cursor = '';
     });
    
  13. Create a map legend style. Add the legend style to your stylesheet.

    
     .legend {
       background-color: #000;
       border-radius: 3px;
       bottom: 30px;
       box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
       color: #fff ;
       font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
       padding: 10px;
       position: absolute;
       right: 10px;
       z-index: 1;
     }
     .legend h4 {
       margin: 0 0 10px;
     }
     .legend div span {
       border-radius: 50%;
       display: inline-block;
       height: 10px;
       margin-right: 5px;
       width: 10px;
     }
    
  14. Create the map legend elements. Add after the element that contains the map.

    
     <div id="state-legend" class="legend">
       <h4>Mean age of </br> women at first marriage</br>in 2019</h4>
         <div><span style="background-color: #fff5eb"></span>23</div>
         <div><span style="background-color: #fee6ce"></span>24</div>
         <div><span style="background-color: #fdd0a2"></span>25</div>
         <div><span style="background-color: #fdae6b"></span>26</div>
         <div><span style="background-color: #fd8d3c"></span>27</div>
         <div><span style="background-color: #f16913"></span>28</div>
         <div><span style="background-color: #d94801"></span>29</div>
         <div><span style="background-color: #8c2d04"></span>30</div>
     </div>
    

Learn more

Get more details about this tutorial on Zoomable Choropleth Map from GeoJSON with MapLibre 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

Join MapTiler Countries with your own custom data and make a choropleth map

How to display an interactive choropleth map legend control

Choropleth legend

Tutorials

Implement an interactive legend control for a choropleth map.

Create a 3D choropleth map of Europe with countries extruded

Create a 3D choropleth map of Europe with countries extruded

Examples

Create 3D choropleth map with extruded country polygons.

Interactive choropleth map

Interactive choropleth map

Examples

Use events and feature states to create a interactive choropleth map.

How to use and filter data for MapTiler Countries

Countries filter

Tutorials

Filter MapTiler Countries data to create choropleth maps.

Was this helpful?