---
title: "Display buildings in 3D | JavaScript maps SDK"
description: "Use extrusions to display height of buildings in 3D."
---

> For AI agents: a documentation index is available at [/llms.txt](https://docs.maptiler.com/llms.txt). Markdown versions of all pages are available by appending .md to any URL.

# Display buildings in 3D

Display the height of buildings in 3D using [extrusions](https://docs.maptiler.com/gl-style-specification/layers/#fill-extrusion).

### NPM module setup

```bash
npm install --save @maptiler/sdk
```

#### main.js

```js
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
);
       
});
```

#### index.html

```html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="robots" content="noindex">
  <title>Display buildings in 3D | NPM example</title>
  <link rel="stylesheet" href="./style.css">
</head>
<body>
  <div id="map"></div>
  <script type="module" src="./main.js"></script>
</body>
</html>
```

#### style.css

```css
body {margin: 0; padding: 0;}
#map {position: absolute; top: 0; bottom: 0; width: 100%;}
```

### Basic JavaScript setup

```html
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Display buildings in 3D</title>
  <script src="https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.umd.min.js"></script>
  <link href="https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.css" rel="stylesheet" />
  <style>
    body {
      margin: 0;
      padding: 0;
    }

    #map {
      position: absolute;
      top: 0;
      bottom: 0;
      width: 100%;
    }
  </style>
</head>

<body>
  <div id="map"></div>
  <script>
    maptilersdk.config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
    const map = new maptilersdk.Map({
      container: 'map',
      style: maptilersdk.MapStyle.BASE,
      center: [-74.0066, 40.7135],
      zoom: 15.5,
      pitch: 45,
      bearing: -17.6,
    });

    // The 'building' layer in the MapTiler Planet vector source contains building-height
    // 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
      );
    });
  </script>
</body>

</html>
```
