Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leaflet 1.0 Upgrade spliceLatLngs alternative?

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!

like image 420
Josef E. Avatar asked Nov 20 '22 12:11

Josef E.


1 Answers

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);
    }
like image 93
Netanel Draiman Avatar answered Dec 10 '22 06:12

Netanel Draiman