Leaflet: getLatLngs used with polygon does not return array

Created on 23 Dec 2016  ·  4Comments  ·  Source: Leaflet/Leaflet

How to reproduce

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

What behaviour I'm expecting and which behaviour I'm seeing

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.

Minimal example reproducing the issue

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.

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 LatLngs. Simply use polygon.getLatLngs()[0] to get the LatLngs of the outer (and only) ring.

All 4 comments

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 LatLngs. Simply use polygon.getLatLngs()[0] to get the LatLngs 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[] ?

Was this page helpful?
0 / 5 - 0 ratings