Hey @willmccartney
I actually stayed with Leaflet. It’s way easier to implement and free… And with ChatGPT you can create the whole logic…
It just takes some custom code:
Import this in the head of the page:
<link href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" rel="stylesheet">
<link href="https://unpkg.com/leaflet-draw@1.0.4/dist/leaflet.draw.css" rel="stylesheet">
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
<script src="https://unpkg.com/leaflet-draw@1.0.4/dist/leaflet.draw.js"></script>
Then add a div with the id=mapid (make sure to give it some width and height. Then add the following js right before the Body tag:
<script>
var mymap = L.map('mapid', {
maxBounds: [
[45.817995, 5.955911], // Southwest coordinates
[47.808455, 10.492340] // Northeast coordinates
],
maxBoundsViscosity: 1.0 // Prevents users from dragging outside the bounds
}).setView([46.8182, 8.2275], 8); // Centered on Switzerland
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {}).addTo(mymap);
var drawnItems = new L.FeatureGroup();
mymap.addLayer(drawnItems);
var drawControl = new L.Control.Draw({
draw: {
polygon: true,
polyline: false,
rectangle: false,
circle: false,
marker: false
}
});
mymap.addControl(drawControl);
// Function to handle the creation of a new polygon
function handlePolygonCreation(layer) {
// Clear existing polygons
drawnItems.clearLayers();
// Add new layer
drawnItems.addLayer(layer);
// Get the coordinates of the polygon and convert them to objects
var coordinates = layer.getLatLngs()[0].map(function(latlng) {
return { lat: latlng.lat, lng: latlng.lng };
});
// Update the variable using WeWeb.io's method
wwLib.wwVariable.updateValue("e301ff65-a460-45af-96f8-b482b596e89e", coordinates);
// Log the updated variable for debugging
console.log('Polygon Coordinates Updated:', coordinates);
}
// Listen for when a new shape is drawn
mymap.on(L.Draw.Event.CREATED, function(event) {
var layer = event.layer;
if (layer instanceof L.Polygon) {
handlePolygonCreation(layer);
}
});
</script>
As you see in the script, there is a variable that gets updated with the coordinates in there:
wwLib.wwVariable.updateValue("e301ff65-a460-45af-96f8-b482b596e89e", coordinates);
Make sure to change the variable UUID to the UUID of your variable (needs to be a variable of type array). With this, you have the values back in a weweb variable, and you can do other stuff with it in weweb…
Only downside is, that you can only test everything when you have published the app… In the editor, custom JS doesn’t work. (little hint there, implement the JS part in an external service like codesandbox for development and then link the js file before the Body tag. That way, you don’t have to republish every time you make changes to the JS.)