Hello there,
I have been having fun building an app. I have decided to use a custom mapbox to allow a draggable point and boundary to update a row in a table. I have butchered together some code from samples on the retool and mapbox forums.
Currently the map loads the point from the model data. I cannot seem to update the model data on a mapbox event handler.
Model data:
{ boundarydata: {},latitude:{{projectQuery.data.latitude[0]}},longitude:{{projectQuery.data.longitude[0]}} }
iframe 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 = 'tokencode';
var draw = new MapboxDraw({
displayControlsDefault: false,
controls: {
polygon: true,
trash: true
}
});
var marker1 = new mapboxgl.Marker({
draggable: true,
color: "red"
});
function updateData(e) {
var data = draw.getAll();
var lat = marker1._lngLat.lat;
var lng = marker1._lgnLat.lng;
window.Retool.modelUpdate({ boundarydata:data, latitude:lat, longitude:lng });
}
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/dark-v10', //hosted style id
zoom: 15 // starting zoom
});
map.addControl(draw);
map.on('draw.create', updateData);
map.on('draw.delete', updateData);
map.on('draw.update', updateData);
map.on('marker1.update', updateData);
window.Retool.subscribe(function(model) {
if (!model) {
return;
}
map.setCenter([parseFloat(model.longitude),parseFloat(model.latitude)]);
marker1.setLngLat([parseFloat(model.longitude),parseFloat(model.latitude)])
marker1.addTo(map);
});
</script>
</body>
</html>
If I create a button to run a query to update the table the new values are not inserted or updated. If I try and create an action to update a text box similarly the new model data is not updated when accessed from the component e.g. {{customMapbox2.model.latitude}}.
Also the issue is that the boundary polygon wont finish on doubleclick like the tutorial.
I used the javascript implemntation window/model mentioned here.
Any help or ideas would be welcome.