geoman-io / leaflet-geoman

🍂🗺️ The most powerful leaflet plugin for drawing and editing geometry layers

Home Page:https://geoman.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Update vertices in draw mode

brunstruts opened this issue · comments

I'm trying to programatically change the latlngs of a shape while drawing it. I am using setLatLngs() on the workingLayer and that works for the line of the shape, but the vertices stays put.

Is there a method for updating the vertices to align with the latlngs?

I have tried enabling drawing and the layer, as proposed in #914, but this only works in edit mode, not drawing mode.

Geoman doesn't support this but maybe with this information you get the right way:

All the markers are stored in the layerGroup and markers, so you can remove all layers from this lists and then recreate all the markers again with _createMarker

_createMarker(latlng) {
// create the new marker
const marker = new L.Marker(latlng, {
draggable: false,
icon: L.divIcon({ className: 'marker-icon' }),
});
this._setPane(marker, 'vertexPane');
marker._pmTempLayer = true;
// add it to the map
this._layerGroup.addLayer(marker);
this._markers.push(marker);

Also checkout the generation / redrawing of the markers in edit mode:

Thank you for the reply
I tried to clear all layers and set new markers (a lot like how it was done in _initMarkers()):

                map.pm.Draw.Line._layerGroup.clearLayers();
                map.pm.Draw.Line._layerGroup = new LayerGroup();
                map.pm.Draw.Line._layerGroup._pmTempLayer = true;

                map.pm.Draw.Line._markers = newCoords.map((latlng) => {
                    return map.pm.Draw.Line._createMarker(latlng);
                });

                map.addLayer(map.pm.Draw.Line._layerGroup);

And the marker was removed! But sadly the path got removed along with it. Updating the path with updatePath() did nothing either. Any more ideas?

I made it work!

while (map.pm.Draw.Line._markers.length > 1) {
          map.pm.Draw.Line._removeLastVertex();
}
newCoords.forEach((latlng) => {
    map.pm.Draw.Line._createVertex({latlng: latlng});
});

This works as long as I don't need the first vertex changed, which so far is not a condition.
Thanks for all the help!