<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Draw Polygons</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://tiles.locationiq.com/v3/libs/maplibre-gl/1.15.2/maplibre-gl.js"></script>
<link href="https://tiles.locationiq.com/v3/libs/maplibre-gl/1.15.2/maplibre-gl.css" rel="stylesheet" />
<script src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.0.0/mapbox-gl-draw.js"></script>
<link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.0.0/mapbox-gl-draw.css" type="text/css" />
<style>
body { margin:0px; padding:0px; }
#map { position:absolute; top:0px; bottom:0px; width:100%; }
</style>
</head>
<body>
<div id="map"></div>
<script>
//Add your LocationIQ Maps Access Token here (not the API token!)
locationiqKey = 'pk.a5c3fbf2119bfb2275b62eddbccd76b3';
//Define the map and configure the map's theme
var map = new maplibregl.Map({
container: 'map',
attributionControl: false, //need this to show a compact attribution icon (i) instead of the whole text
style: 'https://tiles.locationiq.com/v3/streets/vector.json?key='+locationiqKey,
zoom: 12,
center: [-122.42, 37.779]
});
//Add draw controls
var draw = new MapboxDraw({
displayControlsDefault: false,
controls: {
polygon: true,
trash: true
}
});
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>