Like you said, you can use the getLatLngs
method of L.Polyline
to access the coordinates and use them to initialize a L.Polygon
. If you need to access the weight that's been set on the polyline you can do so by using it's options object:
var polyline = new L.Polyline([[25, -25], [25, 25], [-25, 25], [-25, -25]], {
weight: 10,
}).addTo(map);
var polygon = new L.Polygon(polyline.getLatLngs(), {
weight: polyline.options.weight
}).addTo(map);
You could even use the entire options object if you've got more things you'll need to duplicate:
var polygon = new L.Polygon(polyline.getLatLngs(), polyline.options).addTo(map);
Since L.Polygon
is extended from L.Polyline
that won't be a problem since it has the same options.