<!-- Replace <YOUR_API_ACCESS_TOKEN> with your LocationIQ token before using this code. -->
<!DOCTYPE html>
<html>
    <head>
        <meta charset='utf-8' />
        <title>Draw Polygons - custom style</title>
        <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
        
        <script src='https://tiles.locationiq.com/v3/libs/maplibre-gl/5.24.0/maplibre-gl.js?v=0.1.9'></script>
        <link href='https://tiles.locationiq.com/v3/libs/maplibre-gl/5.24.0/maplibre-gl.css?v=0.1.9' rel='stylesheet' />
        
        <script src='https://tiles.locationiq.com/v3/libs/liq/liq-styles-ctrl-libre-gl-v5.js?v=0.1.9'></script>
        <link href='https://tiles.locationiq.com/v3/css/liq-styles-ctrl-libre-gl.css?v=0.1.9' rel="stylesheet" />
        
        <!-- 
            You need to include the mapbox-gl draw plugin to have interactive drawing controls
        -->
        
        <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:0px; padding:0px; }
            #map { position:absolute; top:0px; bottom:0px; width:100%; }
        </style>
    </head>

    <body>
        <div id='map'></div>
        <script>

            MapboxDraw.constants.classes.CANVAS = 'maplibregl-canvas';
            MapboxDraw.constants.classes.CONTROL_BASE = 'maplibregl-ctrl';
            MapboxDraw.constants.classes.CONTROL_PREFIX = 'maplibregl-ctrl-';
            MapboxDraw.constants.classes.CONTROL_GROUP = 'maplibregl-ctrl-group';
            MapboxDraw.constants.classes.ATTRIBUTION = 'maplibregl-ctrl-attrib';
            
            //Add your LocationIQ Access Token here - https://my.locationiq.com/
            locationiq.key = '<YOUR_API_ACCESS_TOKEN>';
            //Define the map and configure the map's theme
            var map = new maplibregl.Map({
                container: 'map',
                style: locationiq.getLayer("Streets"),
                zoom: 12,
                center: [-122.42, 37.779]
            });
                        
            //Add draw controls
 
            var draw = new MapboxDraw({
               displayControlsDefault: false,
               controls: {
                   polygon: true,
                   trash: true
               },
                //This is OPTIONAL, only to show how to customize the style of the shapes (points, lines). More info here: https://github.com/mapbox/mapbox-gl-draw/blob/master/docs/EXAMPLES.md               
                styles: [
                    // ACTIVE (being drawn)
                    // line stroke
                    {
                        "id": "gl-draw-line",
                        "type": "line",
                        "filter": ["all", ["==", "$type", "LineString"], ["!=", "mode", "static"]],
                        "layout": {
                          "line-cap": "round",
                          "line-join": "round"
                        },
                        "paint": {
                          "line-color": "#D20C0C",
                          "line-dasharray": [0.2, 2],
                          "line-width": 2
                        }
                    },
                    // polygon fill
                    {
                      "id": "gl-draw-polygon-fill",
                      "type": "fill",
                      "filter": ["all", ["==", "$type", "Polygon"], ["!=", "mode", "static"]],
                      "paint": {
                        "fill-color": "#D20C0C",
                        "fill-outline-color": "#D20C0C",
                        "fill-opacity": 0.1
                      }
                    },
                    // polygon outline stroke
                    // This doesn't style the first edge of the polygon, which uses the line stroke styling instead
                    {
                      "id": "gl-draw-polygon-stroke-active",
                      "type": "line",
                      "filter": ["all", ["==", "$type", "Polygon"], ["!=", "mode", "static"]],
                      "layout": {
                        "line-cap": "round",
                        "line-join": "round"
                      },
                      "paint": {
                        "line-color": "#D20C0C",
                        "line-dasharray": [0.2, 2],
                        "line-width": 2
                      }
                    },
                    // vertex point halos
                    {
                      "id": "gl-draw-polygon-and-line-vertex-halo-active",
                      "type": "circle",
                      "filter": ["all", ["==", "meta", "vertex"], ["==", "$type", "Point"], ["!=", "mode", "static"]],
                      "paint": {
                        "circle-radius": 5,
                        "circle-color": "#FFF"
                      }
                    },
                    // vertex points
                    {
                      "id": "gl-draw-polygon-and-line-vertex-active",
                      "type": "circle",
                      "filter": ["all", ["==", "meta", "vertex"], ["==", "$type", "Point"], ["!=", "mode", "static"]],
                      "paint": {
                        "circle-radius": 3,
                        "circle-color": "#D20C0C",
                      }
                    },

                    // INACTIVE (static, already drawn)
                    // line stroke
                    {
                        "id": "gl-draw-line-static",
                        "type": "line",
                        "filter": ["all", ["==", "$type", "LineString"], ["==", "mode", "static"]],
                        "layout": {
                          "line-cap": "round",
                          "line-join": "round"
                        },
                        "paint": {
                          "line-color": "#000",
                          "line-width": 3
                        }
                    },
                    // polygon fill
                    {
                      "id": "gl-draw-polygon-fill-static",
                      "type": "fill",
                      "filter": ["all", ["==", "$type", "Polygon"], ["==", "mode", "static"]],
                      "paint": {
                        "fill-color": "#000",
                        "fill-outline-color": "#000",
                        "fill-opacity": 0.1
                      }
                    },
                    // polygon outline
                    {
                      "id": "gl-draw-polygon-stroke-static",
                      "type": "line",
                      "filter": ["all", ["==", "$type", "Polygon"], ["==", "mode", "static"]],
                      "layout": {
                        "line-cap": "round",
                        "line-join": "round"
                      },
                      "paint": {
                        "line-color": "#000",
                        "line-width": 3
                      }
                    }
                ]
               
            });
           
            map.addControl(draw);

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

            //This function is called when a draw event is triggered (create, delete, update)
            //API ref for all that you can do with the draw control: https://github.com/mapbox/mapbox-gl-draw/blob/master/docs/API.md
            function updateArea(e) {
                var data = draw.getAll();
                //We are simply logging all the geometries into console
                //Your first shape's points data can be found in features->0->geometry->coordinates
                console.log(data);
                //You can use http://turfjs.org/ to get bounding boxes, area, etc for these shapes
            }
            
        </script>
    </body>
</html>