How to create a countries map with your own data
This tutorial shows how to join the MapTiler Countries dataset with your own custom data and make a choropleth map. MapTiler Countries is a dataset that primarily contains data about administrative divisions of a world based on countries and their territories.
Copy the following code, paste it into your favorite text editor, and save it as a
Install the npm package.
Include the CSS file.
YOUR_MAPTILER_API_KEY_HEREwith 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 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.
Add event handler for map
loadevent. You will add code to create a vector source and a vector layer in this handler.
Create vector source. The following snippet creates vector source for the MapTiler Countries dataset.
Get the id of the first symbol layer. We want to include the vector layer below the map labels. That means we need to know the id of the first symbol layer so we can include the vector layer before this layer.
Add the vector layer. We need to include
map.addLayerfunction to display the vector under the maps labels
Filter the data to only show the countries. Add a filter to the layer to only display level 0 items.
Prepare your data. Each feature in the dataset has its own identification for pairing. You need to mark your data with the same identifiers as is stored by features. We will use the iso_n3 identifier for countries. Alternatively, you can use one of the properties: name, country codes (ISO A2), or wiki data-id. Check out the Countries dataset schema to see the available fields.
Join the data to coresponding features. Use
querySourceFeaturesto get all features from the layer for visualization. We are also filtering for all countries with
['==', 'level', 0]using style expressions. The second part of the following code goes throw these features and adds attributes from our data array (vizData) to relevant features.
Create a function that validates that the country dataset is loaded to call the
Add event handler for map
dataevent. Call the
Create a choropleth map based on the population attribute. Change the
fill-colorproperty of the layer.
Display a popup when clicking on the vector layer and show the information of the age attribute.
To make our map more user friendly, we will change the cursor when hovering over a geometry in the vector layer to indicate to the user that they can click on it.
Get more details about this tutorial on Join MapTiler Countries with your own custom data and make a choropleth map
Check out the tutorials How to use and filter data for MapTiler Countries and How to display an interactive choropleth map legend control.
Visit the MapTiler Countries schema to know how it is organized into different thematic layers and which attribute and values each layer contains.
Read the following How-tos related to choropleth maps:
Interactive choropleth mapExample
Use events and feature states to create a interactive choropleth map.
Point filtering by propertyExample
Filter thousands of features in real time to quickly and easily find those with a property that meets your search criteria.