Leaflet 1.0 Update spliceLatLngs alternatives?

Prior to Leaflet 1.0, I used the spliceLatLngs method to place coordinates inside a polyline object, for example:

line.spliceLatLngs(index, 1, new L.LatLng(lat, lng)); 

However, with a recent update, spliceLatLngs has been reorganized due to the way the new polylines have nested coordinate arrays.

I tried to make such an alternative:

 var latLngs = line.getLatLngs(); // Because I don't need the multi-dimensional array here if (line instanceof L.Polygon) { latLngs = latLngs[0]; } if (data) { latLngs.splice(index, 1, data); } else { latLngs.splice(index, 1); } line.redraw(); 

But with this alternative, it seems to get rid of the final coordinate. :(

Did I just skip something obvious to connect the coordinate to an existing polyline?

Thank you in advance!

+5
source share
1 answer

You can create a helper function that replaces spliceLatLngs , here is an example written in Typescript:

  spliceLatLngs(polyline: L.Polyline, start: number, deleteCount: number, ...items: L.LatLng[]) { const latlngs: L.LatLng[] = polyline.getLatLngs(); latlngs.splice(start, deleteCount, ...items); polyline.setLatLngs(latlngs); } 
0
source

Source: https://habr.com/ru/post/1259742/


All Articles