code
Snippet
language
CDN
package_2
NPM
content_copy
Copy
open_in_new
Open
const map = new maplibregl. Map ({
container: 'map' ,
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY' ,
center: [ 14.4205 , 50.0875 ],
zoom: 16 ,
pitch: 55 ,
bearing: - 20 ,
canvasContextAttributes: { antialias: true }
});
map. addControl ( new maplibregl. NavigationControl ());
map. on ( 'load' , () => {
const style = map. getStyle ();
// Skip attribution-only vector sources (no url/tiles) such as maptiler_attribution.
const vectorSrc = Object. keys (style.sources). find ( k =>
style.sources[k].type === 'vector' && (style.sources[k].url || style.sources[k].tiles));
if ( ! vectorSrc) return ;
// Insert the 3D layer BEFORE the first label layer so labels stay on top.
const labelLayerId = style.layers. find ( l => l.type === 'symbol' && l.layout && l.layout[ 'text-field' ])?.id;
map. addLayer ({
id: '3d-buildings' ,
source: vectorSrc,
'source-layer' : 'building' ,
type: 'fill-extrusion' ,
minzoom: 14 ,
filter: [ '!=' , [ 'get' , 'hide_3d' ], true ],
paint: {
'fill-extrusion-color' : '#aaa' ,
'fill-extrusion-height' : [
'interpolate' , [ 'linear' ], [ 'zoom' ],
14 , 0 ,
16 , [ 'coalesce' , [ 'get' , 'render_height' ], 0 ]
],
'fill-extrusion-base' : [ 'coalesce' , [ 'get' , 'render_min_height' ], 0 ],
'fill-extrusion-opacity' : 0.7
}
}, labelLayerId);
}); content_copy
Copy
open_in_new
Open
<! doctype html >
< html lang = "en" >
< head >
< meta charset = "utf-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
< title >3D Building Extrusions with MapLibre GL JS</ title >
< link rel = "stylesheet" href = "https://unpkg.com/maplibre-gl@6.16.0/dist/maplibre-gl.css" >
< style >
body { margin : 0 ; padding : 0 ; }
#map { width : 100 % ; height : 100 vh ; }
</ style >
</ head >
< body >
< div id = "map" ></ div >
< script type = "module" >
import * as maplibregl from 'https://unpkg.com/maplibre-gl@6.16.0/dist/maplibre-gl.mjs' ;
const map = new maplibregl. Map ({
container: 'map' ,
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY' ,
center: [ 14.4205 , 50.0875 ],
zoom: 16 ,
pitch: 55 ,
bearing: - 20 ,
canvasContextAttributes: { antialias: true }
});
map. addControl ( new maplibregl. NavigationControl ());
map. on ( 'load' , () => {
const style = map. getStyle ();
// Skip attribution-only vector sources (no url/tiles) such as maptiler_attribution.
const vectorSrc = Object. keys (style.sources). find ( k =>
style.sources[k].type === 'vector' && (style.sources[k].url || style.sources[k].tiles));
if ( ! vectorSrc) return ;
// Insert the 3D layer BEFORE the first label layer so labels stay on top.
const labelLayerId = style.layers. find ( l => l.type === 'symbol' && l.layout && l.layout[ 'text-field' ])?.id;
map. addLayer ({
id: '3d-buildings' ,
source: vectorSrc,
'source-layer' : 'building' ,
type: 'fill-extrusion' ,
minzoom: 14 ,
filter: [ '!=' , [ 'get' , 'hide_3d' ], true ],
paint: {
'fill-extrusion-color' : '#aaa' ,
'fill-extrusion-height' : [
'interpolate' , [ 'linear' ], [ 'zoom' ],
14 , 0 ,
16 , [ 'coalesce' , [ 'get' , 'render_height' ], 0 ]
],
'fill-extrusion-base' : [ 'coalesce' , [ 'get' , 'render_min_height' ], 0 ],
'fill-extrusion-opacity' : 0.7
}
}, labelLayerId);
});
</ script >
</ body >
</ html > content_copy
Copy
open_in_new
Open
import maplibregl from 'maplibre-gl' ;
import 'maplibre-gl/dist/maplibre-gl.css' ;
const map = new maplibregl. Map ({
container: 'map' ,
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY' ,
center: [ 14.4205 , 50.0875 ],
zoom: 16 ,
pitch: 55 ,
bearing: - 20 ,
canvasContextAttributes: { antialias: true }
});
map. addControl ( new maplibregl. NavigationControl ());
map. on ( 'load' , () => {
const style = map. getStyle ();
// Skip attribution-only vector sources (no url/tiles) such as maptiler_attribution.
const vectorSrc = Object. keys (style.sources). find ( k =>
style.sources[k].type === 'vector' && (style.sources[k].url || style.sources[k].tiles));
if ( ! vectorSrc) return ;
// Insert the 3D layer BEFORE the first label layer so labels stay on top.
const labelLayerId = style.layers. find ( l => l.type === 'symbol' && l.layout && l.layout[ 'text-field' ])?.id;
map. addLayer ({
id: '3d-buildings' ,
source: vectorSrc,
'source-layer' : 'building' ,
type: 'fill-extrusion' ,
minzoom: 14 ,
filter: [ '!=' , [ 'get' , 'hide_3d' ], true ],
paint: {
'fill-extrusion-color' : '#aaa' ,
'fill-extrusion-height' : [
'interpolate' , [ 'linear' ], [ 'zoom' ],
14 , 0 ,
16 , [ 'coalesce' , [ 'get' , 'render_height' ], 0 ]
],
'fill-extrusion-base' : [ 'coalesce' , [ 'get' , 'render_min_height' ], 0 ],
'fill-extrusion-opacity' : 0.7
}
}, labelLayerId);
});
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY',
center: [14.4205, 50.0875],
zoom: 16,
pitch: 55,
bearing: -20,
canvasContextAttributes: { antialias: true }
});
map.addControl(new maplibregl.NavigationControl());
map.on('load', () => {
const style = map.getStyle();
// Skip attribution-only vector sources (no url/tiles) such as maptiler_attribution.
const vectorSrc = Object.keys(style.sources).find(k =>
style.sources[k].type === 'vector' && (style.sources[k].url || style.sources[k].tiles));
if (!vectorSrc) return;
// Insert the 3D layer BEFORE the first label layer so labels stay on top.
const labelLayerId = style.layers.find(l => l.type === 'symbol' && l.layout && l.layout['text-field'])?.id;
map.addLayer({
id: '3d-buildings',
source: vectorSrc,
'source-layer': 'building',
type: 'fill-extrusion',
minzoom: 14,
filter: ['!=', ['get', 'hide_3d'], true],
paint: {
'fill-extrusion-color': '#aaa',
'fill-extrusion-height': [
'interpolate', ['linear'], ['zoom'],
14, 0,
16, ['coalesce', ['get', 'render_height'], 0]
],
'fill-extrusion-base': ['coalesce', ['get', 'render_min_height'], 0],
'fill-extrusion-opacity': 0.7
}
}, labelLayerId);
}); <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Building Extrusions with MapLibre GL JS</title>
<link rel="stylesheet" href="https://unpkg.com/maplibre-gl@6.16.0/dist/maplibre-gl.css">
<style>
body { margin: 0; padding: 0; }
#map { width: 100%; height: 100vh; }
</style>
</head>
<body>
<div id="map"></div>
<script type="module">
import * as maplibregl from 'https://unpkg.com/maplibre-gl@6.16.0/dist/maplibre-gl.mjs';
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY',
center: [14.4205, 50.0875],
zoom: 16,
pitch: 55,
bearing: -20,
canvasContextAttributes: { antialias: true }
});
map.addControl(new maplibregl.NavigationControl());
map.on('load', () => {
const style = map.getStyle();
// Skip attribution-only vector sources (no url/tiles) such as maptiler_attribution.
const vectorSrc = Object.keys(style.sources).find(k =>
style.sources[k].type === 'vector' && (style.sources[k].url || style.sources[k].tiles));
if (!vectorSrc) return;
// Insert the 3D layer BEFORE the first label layer so labels stay on top.
const labelLayerId = style.layers.find(l => l.type === 'symbol' && l.layout && l.layout['text-field'])?.id;
map.addLayer({
id: '3d-buildings',
source: vectorSrc,
'source-layer': 'building',
type: 'fill-extrusion',
minzoom: 14,
filter: ['!=', ['get', 'hide_3d'], true],
paint: {
'fill-extrusion-color': '#aaa',
'fill-extrusion-height': [
'interpolate', ['linear'], ['zoom'],
14, 0,
16, ['coalesce', ['get', 'render_height'], 0]
],
'fill-extrusion-base': ['coalesce', ['get', 'render_min_height'], 0],
'fill-extrusion-opacity': 0.7
}
}, labelLayerId);
});
</script>
</body>
</html> import maplibregl from 'maplibre-gl';
import 'maplibre-gl/dist/maplibre-gl.css';
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY',
center: [14.4205, 50.0875],
zoom: 16,
pitch: 55,
bearing: -20,
canvasContextAttributes: { antialias: true }
});
map.addControl(new maplibregl.NavigationControl());
map.on('load', () => {
const style = map.getStyle();
// Skip attribution-only vector sources (no url/tiles) such as maptiler_attribution.
const vectorSrc = Object.keys(style.sources).find(k =>
style.sources[k].type === 'vector' && (style.sources[k].url || style.sources[k].tiles));
if (!vectorSrc) return;
// Insert the 3D layer BEFORE the first label layer so labels stay on top.
const labelLayerId = style.layers.find(l => l.type === 'symbol' && l.layout && l.layout['text-field'])?.id;
map.addLayer({
id: '3d-buildings',
source: vectorSrc,
'source-layer': 'building',
type: 'fill-extrusion',
minzoom: 14,
filter: ['!=', ['get', 'hide_3d'], true],
paint: {
'fill-extrusion-color': '#aaa',
'fill-extrusion-height': [
'interpolate', ['linear'], ['zoom'],
14, 0,
16, ['coalesce', ['get', 'render_height'], 0]
],
'fill-extrusion-base': ['coalesce', ['get', 'render_min_height'], 0],
'fill-extrusion-opacity': 0.7
}
}, labelLayerId);
});
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Building Extrusions with MapLibre GL JS</title>
<link rel="stylesheet" href="https://unpkg.com/maplibre-gl@6.16.0/dist/maplibre-gl.css">
<style>
body { margin: 0; padding: 0; }
#map { width: 100%; height: 100vh; }
</style>
</head>
<body>
<div id="map"></div>
<script type="module">
import * as maplibregl from 'https://unpkg.com/maplibre-gl@6.16.0/dist/maplibre-gl.mjs';
window.maplibregl = maplibregl;
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=LKv9WPh4aJggV1CHU4Y8',
center: [14.4205, 50.0875],
zoom: 16,
pitch: 55,
bearing: -20,
canvasContextAttributes: { antialias: true }
});
map.addControl(new maplibregl.NavigationControl());
map.on('load', () => {
const style = map.getStyle();
// Skip attribution-only vector sources (no url/tiles) such as maptiler_attribution.
const vectorSrc = Object.keys(style.sources).find(k =>
style.sources[k].type === 'vector' && (style.sources[k].url || style.sources[k].tiles));
if (!vectorSrc) return;
// Insert the 3D layer BEFORE the first label layer so labels stay on top.
const labelLayerId = style.layers.find(l => l.type === 'symbol' && l.layout && l.layout['text-field'])?.id;
map.addLayer({
id: '3d-buildings',
source: vectorSrc,
'source-layer': 'building',
type: 'fill-extrusion',
minzoom: 14,
filter: ['!=', ['get', 'hide_3d'], true],
paint: {
'fill-extrusion-color': '#aaa',
'fill-extrusion-height': [
'interpolate', ['linear'], ['zoom'],
14, 0,
16, ['coalesce', ['get', 'render_height'], 0]
],
'fill-extrusion-base': ['coalesce', ['get', 'render_min_height'], 0],
'fill-extrusion-opacity': 0.7
}
}, labelLayerId);
});
</script>
</body>
</html>