How to create a choropleth Map from GeoJSON using Leaflet
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.
-
Copy the following code, paste it into your favorite text editor, and save it as a
.html
file. -
Replace
YOUR_MAPTILER_API_KEY_HERE
with your actual MapTiler API key. -
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 thestarting zoom
) to match your users’ needs. Additionally, you can change the map’s look (by updating thesource 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. -
Include the Leaflet-AJAX plugin JavaScript file in the
<head>
of your HTML file. To add a GeoJSON from an external URL to the leaflet, we will need to use theLeaflet-AJAX plugin
. -
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 for average age at first marriage
-
Create the functions to get the style based on the age attribute.
-
Create a choropleth map based on the age attribute. Change the
style
property 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.
Learn more
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