Skip to content

Add Contour Lines

Contour lines show elevation intervals as lines on the map, making it easy to read the shape of the terrain at a glance. In Maptoolkit Maps JS, you generate contours by adding a raster-dem source and connecting it to a contour source type, then styling the result as a line layer. Use contour lines in hiking, cycling, or outdoor activity apps where understanding slope and elevation change is important.

const API_KEY = 'YOUR_API_KEY';

    const map = new maptoolkit.Map({
        container: 'map',
        apiKey: API_KEY,
        style: `https://styles.maptoolkit.net/maptoolkit/maptoolkit.summer.json?api_key=${API_KEY}`,
        center: [11.39085, 47.27574],
        zoom: 10,
        attributionControl: { compact: false }
    });

    map.addControl(new maptoolkit.NavigationControl(), 'top-right');

    map.on('load', () => {
        map.addSource('terrain-dem', {
            type: 'raster-dem',
            tiles: [`https://vtc-cdn.maptoolkit.net/terrain/{z}/{x}/{y}.webp?api_key=${API_KEY}`],
            tileSize: 256,
            minzoom: 5,
            maxzoom: 12,
            encoding: 'terrarium'
        });

        const beforeId = (map.getStyle().layers.find((l) => /^road|^building/.test(l.id)) || {}).id;

        map.addLayer({
            id: 'contours',
            type: 'hillshade',
            source: 'terrain-dem',
            paint: {
                'hillshade-exaggeration': 0.3,
                'hillshade-shadow-color': '#3d2b1f',
                'hillshade-highlight-color': '#fff'
            }
        }, beforeId);
    });
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Add Contour Lines - Maptoolkit Maps JS</title>
    <meta property="og:description" content="Display elevation contour lines using a raster-dem source." />
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://unpkg.com/@maptoolkit/[email protected]/dist/maptoolkit.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/@maptoolkit/[email protected]/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 = 'YOUR_API_KEY';

    const map = new maptoolkit.Map({
        container: 'map',
        apiKey: API_KEY,
        style: `https://styles.maptoolkit.net/maptoolkit/maptoolkit.summer.json?api_key=${API_KEY}`,
        center: [11.39085, 47.27574],
        zoom: 10,
        attributionControl: { compact: false }
    });

    map.addControl(new maptoolkit.NavigationControl(), 'top-right');

    map.on('load', () => {
        map.addSource('terrain-dem', {
            type: 'raster-dem',
            tiles: [`https://vtc-cdn.maptoolkit.net/terrain/{z}/{x}/{y}.webp?api_key=${API_KEY}`],
            tileSize: 256,
            minzoom: 5,
            maxzoom: 12,
            encoding: 'terrarium'
        });

        const beforeId = (map.getStyle().layers.find((l) => /^road|^building/.test(l.id)) || {}).id;

        map.addLayer({
            id: 'contours',
            type: 'hillshade',
            source: 'terrain-dem',
            paint: {
                'hillshade-exaggeration': 0.3,
                'hillshade-shadow-color': '#3d2b1f',
                'hillshade-highlight-color': '#fff'
            }
        }, beforeId);
    });
</script>
</body>
</html>

Use the prompt below with any LLM to get the same result. Make sure the Maptoolkit MCP server is connected first — check out AI Integration & MCP to get started.

Use the Maptoolkit Connector. Create an interactive map with zoom level 10, centered around [11.39085, 47.27574], displaying elevation contour lines.