Display buildings in 3D

Display the height of buildings in 3D using extrusions.

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.BASE, 
    center: [-74.0066, 40.7135],
    zoom: 15.5,
    pitch: 45,
    bearing: -17.6
});

    // The 'building' layer in the streets vector source contains building-height
    // data from MapTiler Planet: https://www.maptiler.com/planet/.
map.on('load', function () {
    // Insert the layer beneath any symbol layer. 
    // Learn how to find specific layer id https://docs.maptiler.com/sdk-js/examples/geojson-layer-in-stack/#learn-more
    const layers = map.getStyle().layers;

    let labelLayerId;
    for (let i = 0; i < layers.length; i++) {
        if (layers[i].type === 'symbol' && layers[i].layout['text-field']) {
            labelLayerId = layers[i].id;
            break;
        }
    }

    map.addLayer(
    {
        "id": "Building 3D",
        "source": "maptiler_planet", // read more about MapTiler Planet: https://docs.maptiler.com/schema/planet/
        "type": "fill-extrusion",
        "source-layer": "building",
        "filter": [
            "!has",
            "hide_3d"
        ], // filter out buildings that should not be displayed in 3D
        "minzoom": 15,
        "paint": {
            "fill-extrusion-color": "hsl(44,14%,79%)",
            "fill-extrusion-height": {
                "property": "height",
                "type": "identity"
            },
            "fill-extrusion-opacity": 0.6
        },

    }, labelLayerId
);
       
});
Was this helpful?