Prior to an upgrade of Leaflet 1.0 I was using the spliceLatLngs method to place coordinates within a polyline object like:
line.spliceLatLngs(index, 1, new L.LatLng(lat, lng));
However with the recent upgrade, spliceLatLngs was refactored out because of how the new polylines are with the nested arrays of coordinates.
I attempted to do a alternative like so:
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 trailing coordinate. :(
Am I just missing something obvious to splice a coordinate into an existing polyline?
Thank you in advance!
You can create a helper function that will replace spliceLatLngs
,
here's 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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With