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.

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

  2. Install the npm package.

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

  4. Include the following code in your JavaScript file (Example: app.js).

  5. Replace YOUR_MAPTILER_API_KEY_HERE with your actual MapTiler API key.

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

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

  8. Create GeoJSON source. The following snippet creates GeoJSON source hosted on MapTiler Cloud (check out the How to Upload GeoJSON to MapTiler Cloud 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.

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

  10. Add the GeoJSON layer. We need to include firsSymbolId on the map.addLayer function to display the GeoJSON under the map’s labels.

  11. Create a choropleth map based on the age attribute. Change the fill-color property of the layer.

  12. Display a popup when clicking on the geojson layer and show the information of the age attribute.

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

  14. Create a map legend style. Add the legend style to your stylesheet.

  15. Create the map legend elements. Add after the element that contains the map.

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 Cloud

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

Related examples

An extension of MapLibre GL JS