Visualize population density

Use a variable binding expression to calculate and display population density.

In this example, we use the population and area variables to create a choropleth map that visually represents the population density.

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', // container id
    style: MapStyle.STREETS,  // stylesheet location
    center: [30.0222, -1.9596], // starting position [lng, lat]
    zoom: 7 // starting zoom
});

map.on('load', function () {
    map.addSource('rwanda-provinces', {
        'type': 'geojson',
        'data':
            'https://docs.maptiler.com/sdk-js/assets/rwanda-provinces.geojson'
    });
    map.addLayer({
        'id': 'rwanda-provinces',
        'type': 'fill',
        'source': 'rwanda-provinces',
        'layout': {},
        'paint': {
            'fill-color': [
                'let',
                'density',
                ['/', ['get', 'population'], ['get', 'sq-km']],
                [
                    'interpolate',
                    ['linear'],
                    ['zoom'],
                    8,
                    [
                        'interpolate',
                        ['linear'],
                        ['var', 'density'],
                        274,
                        ['to-color', '#edf8e9'],
                        1551,
                        ['to-color', '#006d2c']
                    ],
                    10,
                    [
                        'interpolate',
                        ['linear'],
                        ['var', 'density'],
                        274,
                        ['to-color', '#eff3ff'],
                        1551,
                        ['to-color', '#08519c']
                    ]
                ]
            ],
            'fill-opacity': 0.7
        }
    });
});
How to create a choropleth Map from GeoJSON

Choropleth GeoJSON

Tutorials

Create a styled choropleth map using a GeoJSON overlay.

Polygon layer (polygon helper)

Polygon layer (polygon helper)

Examples

Add minimal polygon layer using MapTiler polygon helper.

Interactive choropleth map

Interactive choropleth map

Examples

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

Show polygon data from GeoJSON on the map

GeoJSON polygon layer

Tutorials

Add GeoJSON polygon overlay to your web map application.

Was this helpful?