Interactive choropleth map

Create an interactive choropleth map by utilizing events and feature states. Utilize events and feature states to develop an engaging choropleth map.

Enhance the level of interaction in your choropleth map by integrating an informative box that showcases the name of the state and its population density. Additionally, learn how to generate a legend that effectively represents the various values and categories depicted on the choropleth map.

NPM module setup


npm install --save @maptiler/sdk
import { Map, MapStyle, config } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';

config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const map = new Map({
    container: 'map',
    style: MapStyle.DATAVIZ.LIGHT, 
    center: [-100.486052, 37.830348],
    zoom: 3
});
let hoveredStateId = null;

map.on('load', function () {
    map.addSource('states', {
        'type': 'geojson',
        'data':
            'https://docs.maptiler.com/sdk-js/assets/us_states.geojson'
    });

    // The feature-state dependent fill-opacity expression will render the hover effect
    // when a feature's hover state is set to true.
    map.addLayer({
        'id': 'state-fills',
        'type': 'fill',
        'source': 'states',
        'layout': {},
        'paint': {
            'fill-color': ["step",
                ["get", "DENSITY"],
                "#FFEDA0", 10,
                "#FED976", 20,
                "#FEB24C", 50,
                "#FD8D3C", 100,
                "#FC4E2A", 200,
                "#E31A1C", 500,
                "#BD0026", 1000,
                "#800026"
            ],
            'fill-opacity': [
                'case',
                ['boolean', ['feature-state', 'hover'], false],
                1,
                0.8
            ],
            'fill-outline-color': "rgba(255, 255, 255, 0)"
        }
    });

    map.addLayer({
        'id': 'state-borders',
        'type': 'line',
        'source': 'states',
        'layout': {},
        'paint': {
            'line-color': '#FFFFFF',
            'line-width': [
                'case',
                ['boolean', ['feature-state', 'hover'], false],
                3,
                1
            ],
        }
    });

    // When the user moves their mouse over the state-fill layer, we'll update the
    // feature state for the feature under the mouse.
    map.on('mousemove', 'state-fills', function (e) {
        if (e.features.length > 0) {
            if (hoveredStateId) {
                map.setFeatureState(
                    { source: 'states', id: hoveredStateId },
                    { hover: false }
                );
            }
            hoveredStateId = e.features[0].id;
            map.setFeatureState(
                { source: 'states', id: hoveredStateId },
                { hover: true }
            );
        }
        document.getElementById('pd').innerHTML = `<h3>${e.features[0].properties.STATE_NAME}</h3>
        <p><strong><em>${e.features[0].properties.DENSITY}</strong> people per square mile</em></p>`;
    });

    // When the mouse leaves the state-fill layer, update the feature state of the
    // previously hovered feature.
    map.on('mouseleave', 'state-fills', function () {
        if (hoveredStateId) {
            map.setFeatureState(
                { source: 'states', id: hoveredStateId },
                { hover: false }
            );
        }
        hoveredStateId = null;
        document.getElementById('pd').innerHTML = `<p>Hover over a state!</p>`;
    });
});
Display a popup on hover

Display a popup on hover

Examples

Show a popup when hovering over a custom marker.

Get features under the mouse pointer

Get features under the mouse pointer

Examples

Show properties of hovered map elements with queryRenderedFeatures.

How to create a countries map with your own data

Countries with data

Tutorials

Join custom data with MapTiler Countries for choropleth maps.

Was this helpful?