How to create a choropleth Map from GeoJSON using Leaflet
This tutorial shows you how to add a styled GeoJSON overlay to your map, adjust the placement of labels, display a popup on click, and create a map legend. We’ll use GeoJSON data from EUROSTAT that includes countries with attributes. To use the same data, download the sample dataset of 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 Leaflet, we will need to use theLeaflet-AJAX plugin
.
-
Install Leaflet-AJAX plugin packages. They make it possible to add a GeoJSON from an external URL to Leaflet.
-
Import the new modules in your JavaScript file (Example: main.js) to add the new functionality.
-
Add the GeoJSON layer. With Leaflet-AJAX it`s possible to fetch GeoJSON data hosted on MapTiler (check out the How to upload GeoJSON tutorial). The GeoJSON example uses data from Eurostat’s Marriage Indicators dataset, filtered to show only countries that have available data on women’s mean age at first marriage in 2019. If you want to use the same sample data, download the dataset.
-
Create the functions to get the style 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.
-
Add the map legend style code to your
css
file. -
Create the map legend in your
html
file.
Now you have a basic choropleth map. It doesn’t include country labels on the top of our GeoJSON layer, so it might lack context. If you want to add the labels, continue with the workflow below.
Display labels over country polygons
On choropleth maps, it’s a good practice to include name labels. It helps users get oriented in the map, especially in areas of the world that are unfamiliar to them.
When we created a choropleth map with Leaflet.js, we basically created an overlay of a basemap, which means that our GeoJSON with marriage data covers the existing country labels. That makes the map challenging to read. Let’s fix it and add the labels back. The picture below shows the difference between a basic choropleth map without labels and a more readable map which we’ll get at the end of this workflow.
- Prepare a basemap and label layer
MapTiler offers several basemap styles, each designed for a specific purpose. The Dataviz style was intended for data visualization, making it a perfect fit for choropleth maps. Dataviz also comes in a dark version, allowing your content to capture the user’s full focus.
- To get Dataviz-dark, go to your MapTiler Cloud account and select New map.
- On tab Simple, select Dataviz and switch to the dark preset.
- Click on Customize.
To display labels over GeoJSON, we will need two separate layers: the basemap without labels, and a new layer with labels. Prepare them in the Customize tool:
- In the left menu, select Layers.
- Switch to Verticality view.
- Hide all symbol layers. (The screenshot below shows the icon that marks symbol layers.)
- Save and publish the map. Now you’ve got a map without symbols (labels), which you can use as a new basemap in the next step.
- Repeat the steps above, but this time hide all non-symbol layers. This creates a label-only map, which we’ll use as the top layer to display the labels.
Note down the map IDs of both your new maps. On the Maps page, click on your map, which takes you to the map preview page. The URL of this page contains an alphanumeric string which is the map ID. Example: https://cloud.maptiler.com/maps/0198eca7-644f-7927-825f-e45f2dee80ac/
- Replace your current basemap with your new map without labels. You’ll need the map ID from the previous step.
- Create the label layer by using the other map you’ve created, with labels only.
- Create a pane for labels.
Panes define in which order the layers get displayed.
For the pane, we define z-index
, which specifies which panes can overlap our new labels pane. With z-index
550, our labels can be overlapped with markers, tooltips, and popups. This table shows the default pane settings:
Pane | Z-index | Description |
---|---|---|
mapPane |
'auto' |
The pane that contains all other map panes |
tilePane |
200 |
Pane for GridLayer s and TileLayer s |
overlayPane |
400 |
Pane for overlay shadows (e.g. Marker shadows) |
shadowPane |
500 |
Pane for vectors (Path s, like Polyline s and Polygon s), ImageOverlay s, VideoOverlay s, circles, and GeoJSONs |
markerPane |
600 |
Pane for Icon s of Marker s |
tooltipPane |
650 |
Pane for Tooltip s |
popupPane |
700 |
Pane for Popup s |
The labels are a raster tile layer that covers the entire map and will capture clicks and touches. If you click anywhere on the map, the web browser will assume that you clicked on the labels layer, not the GeoJSON layer, and the pop-up will not appear. We can fix the pop-up appearance by using the pointer-events
. Tip: If you want to avoid this issue completely, switch to a different mapping library (e.g. MapTiler SDK or Openlayers) that supports vector tiles by default. Raster tiles return a raster layer (an image containing all layers) while vector tiles returns geometries; the benefit of geometries is that the layers stay separate and can be easily styled on the fly.
- Assign a label pane to the label tiles definition.
Now your choropleth map should show country name labels nicely.
Learn more
Check out the following how-tos related to choropleth maps:
- How to style a choropleth map in the visual editor Customize
- How to prepare GeoJSON with attributes for choropleth map and upload it to MapTiler