How to create a choropleth Map from GeoJSON

This tutorial shows how to add a styled GeoJSON overlay to the map, display a popup on click, and create a map legend. As an example data, we use countries with attributes from EUROSTAT in GeoJSON format. Download the sample data here

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

  2. Replace YOUR_MAPTILER_API_KEY_HERE with your actual MapTiler API key.

  3. The next it’s up to you. You can start the map in a different place by modifying the starting position and starting zoom, and you can change the look of the map to any of our styles, or yours, by updating the style URL. See what’s available here.

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

  5. 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 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 sample data here

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

  7. Add the GeoJSON layer. We need to include firsSymbolId to the map.addLayer function to display the GeoJSON under the maps labels

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

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

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

  11. Create a map legend.

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