How to use and filter data for MapTiler Countries

This tutorial shows the process of utilizing and refining data from the MapTiler Countries to create a Choropleth map of the US states. The MapTiler Countries dataset primarily consists of information regarding the administrative divisions within various countries and their respective territories.

NPM module setup

  1. Install the npm package.

    
     npm install --save @maptiler/sdk
    
  2. Include the CSS file.

    If you have a bundler that can handle CSS, you can import the CSS

    
     import "@maptiler/sdk/dist/maptiler-sdk.css";
    

    or include it with a <link /> in the head of the document via the CDN

    
     <link href='https://cdn.maptiler.com/maptiler-sdk-js/v4.1.0/maptiler-sdk.css' rel='stylesheet' />
    
  3. Include the following code in your JavaScript file (Example: app.js).

    
     import * as maptilersdk from '@maptiler/sdk';
    
     maptilersdk.config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
     const map = new maptilersdk.Map({
       container: 'map', // container's id or the HTML element to render the map
       style: maptilersdk.MapStyle.STREETS,
          
       center: [-94.28, 38.45], // starting position [lng, lat]
       zoom: 3, // starting zoom
          
     });
    
  4. Replace YOUR_MAPTILER_API_KEY_HERE with your own API key. Make sure to secure the key before you publish it.

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

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

    
     map.on('load', function() {
        
     });
    
  7. Create vector source. The following snippet creates a vector source for the MapTiler Countries dataset.

    
     map.on('load', function() {
       map.addSource('statesData', {
         type: 'vector',
         url: `https://api.maptiler.com/tiles/countries/tiles.json`
       });
     });
    
  8. 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.

    
     // Find the id of the first symbol layer in the map style
     const layers = map.getStyle().layers;
     const firstSymbolId = layers.find(layer => layer.type === 'symbol').id;
    
  9. Add the vector layer. We need to include firsSymbolId on the map.addLayer function to display the vector under the maps labels

    
     map.addLayer(
       {
         'id': 'states',
         'source': 'statesData',
         'source-layer': 'administrative',
         'type': 'fill',
         'paint': {
             'fill-color': '#6B7C93',
             'fill-opacity': 1,
             'fill-outline-color': '#000'
         }
       },
       firstSymbolId   
     );
    
  10. Filter the data to only show the states of USA. Add a filter to the layer to only display level 1 items. The dataset now contains divisions and sub-subdivisions only for the US, if you need other country’s divisions, please contact us.

    
     map.addLayer(
       {
         'id': 'countries',
         'source': 'statesData',
         'source-layer': 'administrative',
         'type': 'fill',
         'filter': [
           'all',
           ['==', 'level', 1],
           ['==', 'level_0', 'US']
         ],
         'paint': {
             'fill-color': '#6B7C93',
             'fill-opacity': 1,
             'fill-outline-color': '#000'
         }
       },
       firstSymbolId   
     );
    
  11. Create a choropleth map based on the name attribute. Change the fill-color property of the layer.

    
     map.addLayer(
       {
         'id': 'countries',
         'source': 'statesData',
         'source-layer': 'administrative',
         'type': 'fill',
         'filter': [
           'all',
           ['==', 'level', 1],
           ['==', 'level_0', 'US']
         ],
         'paint': {
             'fill-color': [
               "match",
               ["get", "name"],
               ["Nebraska", "Alaska", "Washington", "Nevada", "New Mexico", "Montana", "Minnesota", "Louisiana", "North Carolina", "Kentucky", "Massachusetts", "Delaware", "Michigan"],
               "#D5CD85",
               ["Oklahoma", "Florida", "Idaho", "Wisconsin", "Arizona", "Tennessee", "Pennsylvania", "New Hampshire", "Rhode Island"],
               "#D58785",
               ["New York", "California", "Wyoming", "Kansas", "Illinois", "Mississippi", "South Carolina", "West Virginia"],
               "#735F91",
               ["Texas", "Georgia", "Utah", "Missouri", "South Dakota", "Ohio", "Maryland", "Vermont"],
               "#567986",
               ["Colorado", "Oregon", "Alabama", "Indiana", "North Dakota", "Iowa", "Arkansas", "Virginia", "New Jersey", "Maine", "Connecticut"],
               "#69A86D",
               "rgba(0, 0, 0, 0.5)"
             ],
             'fill-opacity': 1,
             'fill-outline-color': '#000'
         }
       },
       firstSymbolId   
     );
    

Learn more

Check out the How to create a countries map with your own data tutorial.

Visit the MapTiler Countries schema to know how it is organized into different thematic layers and which attributes and values each layer contains.

Read 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

How to create a countries map with your own data

Countries with data

Tutorials

Join custom data with MapTiler Countries for choropleth maps.

Point filtering by property

Point filtering by property

Examples

Filter features in real time by property.

Interactive choropleth map

Interactive choropleth map

Examples

Use events and feature states to create a interactive choropleth map.

Was this helpful?