Skip to content

Draw geometries with terra-draw

Use maplibre-gl-terradraw to draw a geometry in various forms such as point, line or polygon on your map.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Draw geometries with terra-draw</title>
    <meta property="og:description" content="Use maplibre-gl-terradraw to draw a geometry in various forms such as point, line or polygon on your map." />
    <meta charset='utf-8'>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel='stylesheet' href='https://unpkg.com/[email protected]/dist/trackasia-gl.css' />
    <script src='https://unpkg.com/[email protected]/dist/trackasia-gl.js'></script>
    <style>
        body { margin: 0; padding: 0; }
        html, body, #map { height: 100%; }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/@watergis/[email protected]/dist/maplibre-gl-terradraw.umd.js"></script>
    <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@watergis/[email protected]/dist/maplibre-gl-terradraw.css"
    />
</head>
<body>

<div id="map"></div>

<script>
    const map = new trackasiagl.Map({
        container: 'map', // container id
        style:
            'https://maps.track-asia.com/styles/v2/streets.json?key=public', //hosted style id
        center: [-91.874, 42.76], // starting position
        zoom: 12 // starting zoom
    });

    // By default, all terra-draw drawing modes are enabled.
    // you can disable some of modes in the constructor options if you want.
    const draw = new MaplibreTerradrawControl.MaplibreTerradrawControl({
        modes: [
            // 'render', comment this to always show drawing tool
            'point',
            'linestring',
            'polygon',
            'rectangle',
            'circle',
            'freehand',
            'angled-rectangle',
            'sensor',
            'sector',
            'select',
            'delete-selection',
            'delete',
            'download'
        ],
        open: true,
    });
    map.addControl(draw, 'top-left');
</script>
</body>
</html>