I'm the author of the geometry editing plugin leaflet.pm and I'm currently investigating a strange bug where editing a polygon sometimes won't update the bounds of the polygon even though the coordinates array changed.
I traced the issue down to two different functions I use to update the coordinates:
layer.setLatlngs().redraw();
and
layer._latlngs = [some new coords]
layer.redraw();
I guess the simple solution is to ALWAYS update a layers coords with setLatLng
– and I'm currently changing my code to do that.
I still want to ask – shouldn't redraw() also update the bounds? Because it is visibly changing the layers appearance to the new coordinates too?
Best Answer
First things first: directly updating private members is never recommended, so API wise using
setLatLngs()
is the way to go.I looked at the source code and the
setLatLng()
method in Polyline.js in order to see why the redraw won't cut it.Polygon
calls thePolyline
setLatLngs()
method so this is where I started.1. setLatLngs method
This implicates that you don't need to call redraw() if you are using this method.
2. The private method
_setLatLngs
:Calls private method
_convertLatLngs
:So, by not using
setLatlngs()
, leaflet did not get the instruction to rebuild the latLng array.