Leaflet: 与多边形一起使用的 getLatLngs 不返回数组

创建于 2016-12-23  ·  4评论  ·  资料来源: Leaflet/Leaflet

如何繁殖

我正在使用的传单版本:1.0.2
我使用的浏览器(带版本):Chrome 和 FF
我使用的操作系统/平台(带版本):Windows 10

我期待什么样的行为以及我看到什么样的行为

与多边形一起使用的 getLatLngs 不返回数组,而是以字符串形式返回 LatLngs。 如果与 LatLng 的折线数组一起使用,则返回。 这适用于 0.7.7 版本。

重现问题的最小示例

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);

使用http://playground-leaflet.rhcloud.com/或任何其他类似 jsfiddle 的站点。

最有用的评论

L.Polygon.getLatLngs()的返回值实际上是一个元素的数组。

这是在https://github.com/Leaflet/Leaflet/blob/b1c2e99c1faf5d7a518a3260c22eb920a7512945/src/layer/vector/Polygon.js#L106 中完成的,因为https://github.com/Leaflet/Leaflet/pull/327有一个戒指列表,而不是LatLng的列表。 只需使用polygon.getLatLngs()[0]即可获取外(且唯一)环的LatLng

所有4条评论

L.Polygon.getLatLngs()的返回值实际上是一个元素的数组。

这是在https://github.com/Leaflet/Leaflet/blob/b1c2e99c1faf5d7a518a3260c22eb920a7512945/src/layer/vector/Polygon.js#L106 中完成的,因为https://github.com/Leaflet/Leaflet/pull/327有一个戒指列表,而不是LatLng的列表。 只需使用polygon.getLatLngs()[0]即可获取外(且唯一)环的LatLng

这说明了一切,非常感谢!

不应该将官方文档http://leafletjs.com/reference-1.0.3.html#polyline -getlatlngs 更改为表示LatLng[]吗?

此页面是否有帮助?
0 / 5 - 0 等级