Skip to content

Draw polygon with mapbox-gl-draw

Use mapbox-gl-draw to draw a polygon and Turf.js to calculate its area in square meters.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Draw polygon with mapbox-gl-draw</title>
    <meta property="og:description" content="Use mapbox-gl-draw to draw a polygon and Turf.js to calculate its area in square meters." />
    <meta charset='utf-8'>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <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>
    <script src="https://www.unpkg.com/@mapbox/[email protected]/dist/mapbox-gl-draw.js"></script>
    <link
    rel="stylesheet"
        href="https://www.unpkg.com/@mapbox/[email protected]/dist/mapbox-gl-draw.css"
    />
    <style>
        body { margin: 0; padding: 0; }
        html, body, #map { height: 100%; }
        .calculation-box {
            height: 75px;
            width: 150px;
            position: absolute;
            bottom: 40px;
            left: 10px;
            background-color: rgba(255, 255, 255, 0.9);
            padding: 15px;
            text-align: center;
        }

        p {
            font-family: 'Open Sans';
            margin: 0;
            font-size: 13px;
        }
    </style>
</head>
<body>
    <div id="map"></div>
    <div class="calculation-box">
        <p>Draw a polygon using the draw tools.</p>
        <div id="calculated-area"></div>
    </div>

    <script type="module">

        import * as turf from 'https://esm.sh/@turf/[email protected]';

        MapboxDraw.constants.classes.CANVAS  = 'trackasiagl-canvas';
        MapboxDraw.constants.classes.CONTROL_BASE  = 'trackasiagl-ctrl';
        MapboxDraw.constants.classes.CONTROL_PREFIX = 'trackasiagl-ctrl-';
        MapboxDraw.constants.classes.CONTROL_GROUP = 'trackasiagl-ctrl-group';
        MapboxDraw.constants.classes.ATTRIBUTION = 'trackasiagl-ctrl-attrib';

        const map = new trackasiagl.Map({
            container: 'map', // container id
            style: 'https://maps.track-asia.com/styles/v2/streets.json?key=public_key', //hosted style id
            center: {"lat":10.762622,"lng":106.660172}, // starting position
            zoom: 12 // starting zoom
        });

        const draw = new MapboxDraw({
            displayControlsDefault: false,
            controls: {
                polygon: true,
                trash: true
            }
        });
        map.addControl(draw);

        map.on('draw.create', updateArea);
        map.on('draw.delete', updateArea);
        map.on('draw.update', updateArea);

        function updateArea(e) {
            const data = draw.getAll();
            const answer = document.getElementById('calculated-area');
            if (data.features.length > 0) {
                const area = turf.area(data);
                // restrict to area to 2 decimal points
                const roundedArea = Math.round(area * 100) / 100;
                answer.innerHTML =
                    `<p><strong>${
                        roundedArea
                    }</strong></p><p>square meters</p>`;
            } else {
                answer.innerHTML = '';
                if (e.type !== 'draw.delete')
                    alert('Use the draw tools to draw a polygon!');
            }
        }
    </script>

</body>
</html>