Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save/load leaflet.draw and use editing/delete buttons #1071

Closed
CORN-EZ opened this issue Jun 13, 2024 · 2 comments
Closed

save/load leaflet.draw and use editing/delete buttons #1071

CORN-EZ opened this issue Jun 13, 2024 · 2 comments

Comments

@CORN-EZ
Copy link

CORN-EZ commented Jun 13, 2024

I've looked into similar questions, but apparently I'm missing something
(#398, #364, #187, #253
https://stackoverflow.com/questions/53463078/leaflet-draw-cannot-read-property-enable-of-undefined-adding-control-to-geoj
https://stackoverflow.com/questions/24018630/how-to-save-a-completed-polygon-points-leaflet-draw-to-mysql-table)

When I create a figure, I attach a bindpopup to it with a button that saves the figure in the database.

I have a layer, activating which I request saved figures from the database, they are successfully loaded, drawn, I place them in drawitems,
but at the same time I do not have the ability to edit newly added layers

I DON'T UNDERSTAND T_T

async function saveText(layerId) {
    var layer = drawnItems.getLayer(layerId);
    var textareaValue = document.querySelector('.leaflet-popup-content textarea').value;
    var type = layer instanceof L.Marker ? 'Point' : 'Polygon';
    var coordinates = type === 'Point' ? layer.getLatLng() : layer.getLatLngs();

    var shape = layer.toGeoJSON()
    var shape_for_db = JSON.stringify(shape);

    var data = {
        type: type,
        coordinate: shape_for_db,
        comment: textareaValue,
    };

    fetch(`${http}://${IP}:/mark/save-mark/`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(data)
    })
}
function view_mark(date) {
    const apiUrl = `${http}://${IP}:/mark/`;
    fetch(apiUrl)
        .then(response => {
            if (!response.ok) {
                throw new Error(` ${response.status}`);
            }
            return response.json();
        })
        .then(data => {
            addMarkersToMap(data);
        })
        .catch(error => {
            console.error('Error:', error);
        });
}
async function addMarkersToMap(data) {
    data.forEach(item => {
        const coordinates = JSON.parse(item.coordinate);
        if (coordinates) {
            if (item.type === 'Point') {
                L.geoJSON(coordinates, {
                pointToLayer: function (feature, latlng) {
                    return L.marker(latlng, {icon: myicon});
                    }
                }).addTo(drawnItems)
                .bindPopup(item.comment);
            } else if (item.type === 'Polygon') {
                    L.geoJSON(coordinates,option).addTo(drawnItems)
                    .bindPopup(item.comment);
            }
        }
    });
}
@CORN-EZ
Copy link
Author

CORN-EZ commented Jun 13, 2024

i also used this code to make sure i don't have groups

function addNonGroupLayers(sourceLayer, targetGroup) {
  if (sourceLayer instanceof L.LayerGroup) {
    sourceLayer.eachLayer(function(layer) {
      addNonGroupLayers(layer, targetGroup);
    });
  } else {
    targetGroup.addLayer(sourceLayer);
  }
}

@CORN-EZ
Copy link
Author

CORN-EZ commented Jun 14, 2024

The problem was solved, if I understood correctly, the problem was that I had previously written data to the database in a different format

and he didn't want to build next

@CORN-EZ CORN-EZ closed this as completed Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant