Thank you for the guidance. Here’s a minimalist implementation of how we successfully extracted the Geojson from the map using vanilla JS.
Add ‘data’ component to Custom Component model. This will hold the generated polygons:
{ data: {} }
This function will update model each time the user draws a polygon (full code below):
function updateData(e) {
var data = draw.getAll();
window.Retool.modelUpdate({ data })
}
Extract the ‘data’ object from the component’s model. Perhaps onto a jsonexplorer.
{{customcomponent1.model.data}}
Custom Component full code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Extract drawn polygon area</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v2.1.1/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.1.1/mapbox-gl.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<script src="https://api.tiles.mapbox.com/mapbox.js/plugins/turf/v3.0.11/turf.min.js"></script>
<script src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.2.1/mapbox-gl-draw.js"></script>
<link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.2.1/mapbox-gl-draw.css" type="text/css">
<div id="map"></div>
<div class="calculation-box">
<p>Draw a polygon using the draw tools.</p>
<div id="calculated-area"></div>
</div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoiZXN0b3JudWRhbWUiLCJhIjoiY2s4YXd3aXNkMDJvNDNkb3diMmJqb3FkYiJ9.bDVBManMcJ7p1zPFQFEw9w';
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/dark-v10', //hosted style id
center: [-99.125519, 19.451054], // starting position
zoom: 8 // starting zoom
});
var draw = new MapboxDraw({
displayControlsDefault: false,
controls: {
polygon: true,
trash: true
}
});
map.addControl(draw);
map.on('draw.create', updateData);
map.on('draw.delete', updateData);
map.on('draw.update', updateData);
function updateData(e) {
var data = draw.getAll();
window.Retool.modelUpdate({ data })
}
</script>
</body>
</html>