Skip to content

Add a vector tile source

Add a vector source to a map.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Add a vector tile source</title>
    <meta property="og:description" content="Add a vector source to a map." />
    <meta charset='utf-8'>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel='stylesheet' href='https://unpkg.com/maplibre-gl@4.7.1/dist/maplibre-gl.css' />
    <script src='https://unpkg.com/maplibre-gl@4.7.1/dist/maplibre-gl.js'></script>
    <style>
        body { margin: 0; padding: 0; }
        html, body, #map { height: 100%; }
    </style>
</head>
<body>
<div id="map"></div>
<script>
    const map = new maplibregl.Map({
        container: 'map',
        style:
            'https://api.maptiler.com/maps/streets/style.json?key=get_your_own_OpIi9ZULNHzrESv6T2vL',
        zoom: 13,
        center: [-122.447303, 37.753574]
    });

    map.on('load', () => {
        map.addSource('contours', {
            type: 'vector',
            url:
                'https://api.maptiler.com/tiles/contours/tiles.json?key=get_your_own_OpIi9ZULNHzrESv6T2vL'
        });
        map.addLayer({
            'id': 'terrain-data',
            'type': 'line',
            'source': 'contours',
            'source-layer': 'contour',
            'layout': {
                'line-join': 'round',
                'line-cap': 'round'
            },
            'paint': {
                'line-color': '#ff69b4',
                'line-width': 1
            }
        });
    });
</script>
</body>
</html>