<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://tiles.locationiq.com/v3/libs/openlayers/8.2.0/ol.css" type="text/css">
<script src="https://tiles.locationiq.com/v3/libs/openlayers/8.2.0/ol.js"></script>
<style>
            body { margin:0px; padding:0px; }
            #map { position:absolute; top:0px; bottom:0px; width:100%; }
        </style>
<script src="https://tiles.locationiq.com/v3/libs/olms/12.0.0/olms.js" type="text/javascript"></script>
<title>Draw Polygons - custom style</title>
</head>
<body>
<div id="map"></div>
<script type="text/javascript">
            var key = 'pk.aa7f5d0539c5675b7f3429402939d8fa';   //Insert your LocationIQ access token here

            var styleJson = 'https://tiles-staging.locationiq.com/v3/streets/vector.json?key=' + key;

            const map = new ol.Map({
                target: 'map',
                view: new ol.View({
                    center: ol.proj.fromLonLat([-122.42, 37.779]),
                    zoom: 12
                })
            });


            //type of feature to draw, Other possible values : 'Point', 'LineString', 'Circle', 'Polygon'
            var featureType = 'Polygon';
            
            //Let’s include the markers and create a vector source with it
            var vectorSource = new ol.source.Vector({});

            //Let’s create a vector layer, with a source created above
            var vectorLayer = new ol.layer.Vector({
                source: vectorSource,
                //This is OPTIONAL, only to show how to customize the style of feature (Polygon,Circle,Line)
                //Changing the style for the vector layer which shares source with the Draw element, will modify the style of the polygon once it is drawn.
                style: new ol.style.Style({
                    fill: new ol.style.Fill({
                        color: 'rgba(210, 12, 12, 0.5)'
                    }),
                    stroke: new ol.style.Stroke({
                        color: 'rgba(210, 12, 12, 0.5)',
                        width: 2
                    })
                }),
                zIndex: 1
            });

            map.addLayer(vectorLayer);

            // draw interaction logic
            // global variable so that we can remove it later
            // To activate freehand drawing for lines, polygons, and circles, hold the Shift key.
            var draw; 
            function addInteraction() {
                draw = new ol.interaction.Draw({
                    source: vectorSource,
                    type: featureType,
                    // This is OPTIONAL, only to show how to customize the style of feature (Polygon,Circle,Line)
                    // Set the style in the Draw element to change the style of the polygon while it is drawn.
                    style: new ol.style.Style({
                        fill: new ol.style.Fill({
                            color: 'rgba(210, 12, 12, 0.5)'
                        }),
                        stroke: new ol.style.Stroke({
                            color: 'rgba(210, 12, 12, 0.5)',
                            width: 2
                        })
                    }),
                    zIndex: 1
                });
                map.addInteraction(draw);
            }
            
            map.onclick = function() {
                map.removeInteraction(draw);
                addInteraction();
            };

            addInteraction();
            
            olms.apply(map, styleJson);
        </script>
</body>
</html>