Extrude Polygons for 3D Indoor Mapping
Use fill-extrusion layers to create a 3D indoor map from a GeoJSON floor plan.
const API_KEY = 'toursprung';
const floorplan = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: { type: 'Polygon', coordinates: [[[11.390, 47.275], [11.392, 47.275], [11.392, 47.277], [11.390, 47.277], [11.390, 47.275]]] },
properties: { level: 1, name: 'Lobby', color: '#aee', height: 4 }
},
{
type: 'Feature',
geometry: { type: 'Polygon', coordinates: [[[11.392, 47.275], [11.394, 47.275], [11.394, 47.277], [11.392, 47.277], [11.392, 47.275]]] },
properties: { level: 1, name: 'Hall A', color: '#fea', height: 6 }
},
{
type: 'Feature',
geometry: { type: 'Polygon', coordinates: [[[11.390, 47.277], [11.394, 47.277], [11.394, 47.279], [11.390, 47.279], [11.390, 47.277]]] },
properties: { level: 2, name: 'Hall B', color: '#eaf', height: 8 }
}
]
};
const map = new maptoolkit.Map({
container: 'map',
style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
center: [11.392, 47.277],
zoom: 16,
pitch: 50,
bearing: 20,
attributionControl: { compact: false }
});
map.addControl(new maptoolkit.NavigationControl(), 'top-right');
map.on('load', () => {
map.addSource('floorplan', { type: 'geojson', data: floorplan });
map.addLayer({
id: 'rooms-extrusion',
type: 'fill-extrusion',
source: 'floorplan',
paint: {
'fill-extrusion-color': ['get', 'color'],
'fill-extrusion-height': ['get', 'height'],
'fill-extrusion-base': 0,
'fill-extrusion-opacity': 0.8
}
});
});<!DOCTYPE html>
<html lang="en">
<head>
<title>Extrude Polygons for 3D Indoor Mapping – Maptoolkit Maps JS</title>
<meta property="og:description" content="Use fill-extrusion layers to create a 3D indoor map from a GeoJSON floor plan." />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://unpkg.com/@maptoolkit/maps@11.0.0-beta.2/dist/maptoolkit.js"></script>
<link rel="stylesheet" href="https://unpkg.com/@maptoolkit/maps@11.0.0-beta.2/dist/maptoolkit.css" />
<style>
html, body { width: 100%; height: 100%; margin: 0; padding: 0; }
#map { width: 100%; height: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script>
const API_KEY = 'toursprung';
const floorplan = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: { type: 'Polygon', coordinates: [[[11.390, 47.275], [11.392, 47.275], [11.392, 47.277], [11.390, 47.277], [11.390, 47.275]]] },
properties: { level: 1, name: 'Lobby', color: '#aee', height: 4 }
},
{
type: 'Feature',
geometry: { type: 'Polygon', coordinates: [[[11.392, 47.275], [11.394, 47.275], [11.394, 47.277], [11.392, 47.277], [11.392, 47.275]]] },
properties: { level: 1, name: 'Hall A', color: '#fea', height: 6 }
},
{
type: 'Feature',
geometry: { type: 'Polygon', coordinates: [[[11.390, 47.277], [11.394, 47.277], [11.394, 47.279], [11.390, 47.279], [11.390, 47.277]]] },
properties: { level: 2, name: 'Hall B', color: '#eaf', height: 8 }
}
]
};
const map = new maptoolkit.Map({
container: 'map',
style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
center: [11.392, 47.277],
zoom: 16,
pitch: 50,
bearing: 20,
attributionControl: { compact: false }
});
map.addControl(new maptoolkit.NavigationControl(), 'top-right');
map.on('load', () => {
map.addSource('floorplan', { type: 'geojson', data: floorplan });
map.addLayer({
id: 'rooms-extrusion',
type: 'fill-extrusion',
source: 'floorplan',
paint: {
'fill-extrusion-color': ['get', 'color'],
'fill-extrusion-height': ['get', 'height'],
'fill-extrusion-base': 0,
'fill-extrusion-opacity': 0.8
}
});
});
</script>
</body>
</html>