How to create a choropleth Map from GeoJSON using Leaflet
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
Copy the following code, paste it into your favorite text editor, and save it as a
YOUR_MAPTILER_API_KEY_HEREwith your actual MapTiler API key.
The next it’s up to you. You can start the map in a different place by modifying the
starting position, 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.
<head>of your HTML file. To add a GeoJSON from an external URL to the leaflet, we will need to use the
Add the GeoJSON layer. The following snippet creates GeoJSON layer hosted on MapTiler Cloud (check out the How to Upload GeoJSON to MapTiler Cloud tutorial). 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
Create the functions to get the style based on the age attribute.
Create a choropleth map based on the age attribute. Change the
styleproperty of the layer.
- Create a function to display a popup when clicking on the geojson layer and show the information of the age attribute
Display a popup when clicking on the geojson layer.
Create a map legend.
View complete source code on GitHub
Get more details about this tutorial on Zoomable Choropleth Map from GeoJSON with Leaflet 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