Leaflet version I'm using: 1.0.2
Browser (with version) I'm using: Chrome & FF
OS/Platform (with version) I'm using: Windows 10
getLatLngs used with polygon does not return array, but LatLngs as string. If used with polyline array of LatLng is returned. This worked fine with 0.7.7 version.
var latlngs = [
[45.51, -122.68],
[37.77, -122.43],
[34.04, -118.2]
];
var polyline = L.polyline(latlngs, {color: 'red'}).addTo(map);
alert(polyline.getLatLngs().length);
var polygon = L.polygon([
[51.509, -0.08],
[51.503, -0.06],
[51.51, -0.047]
]).addTo(map);
alert(polygon.getLatLngs().length);
Using http://playground-leaflet.rhcloud.com/ or any other jsfiddle like site.
jsfiddle is here https://jsfiddle.net/qccxwp64/2/
The return value of L.Polygon.getLatLngs()
is, in fact, an array of one element.
This is done in https://github.com/Leaflet/Leaflet/blob/b1c2e99c1faf5d7a518a3260c22eb920a7512945/src/layer/vector/Polygon.js#L106 since https://github.com/Leaflet/Leaflet/pull/3279 , to ensure that there is a list of rings, not a list of LatLng
s. Simply use polygon.getLatLngs()[0]
to get the LatLng
s of the outer (and only) ring.
This explains everything, thank you very much!
Shouldn't the official documentation http://leafletjs.com/reference-1.0.3.html#polyline-getlatlngs be changed to indicate something else than LatLng[]
?
Most helpful comment
The return value of
L.Polygon.getLatLngs()
is, in fact, an array of one element.This is done in https://github.com/Leaflet/Leaflet/blob/b1c2e99c1faf5d7a518a3260c22eb920a7512945/src/layer/vector/Polygon.js#L106 since https://github.com/Leaflet/Leaflet/pull/3279 , to ensure that there is a list of rings, not a list of
LatLng
s. Simply usepolygon.getLatLngs()[0]
to get theLatLng
s of the outer (and only) ring.