Leaflet 传单中的层周长.js

Leaflet 传单中的层周长.js,leaflet,mapbox,Leaflet,Mapbox,你知道如何在MapBox/ployate.js中获得图层中形状的周长吗?我设法使用了这个区域(尽管有时是负数!?)。但它没有周长/周长 谢谢 对于L.Circle可以从其半径计算周长: L.Circle.include({ circumference: function () { return 2 * Math.PI * this.getRadius(); } }); var circle = new L.Circle(...), circumferen

你知道如何在MapBox/ployate.js中获得图层中形状的周长吗?我设法使用了这个区域(尽管有时是负数!?)。但它没有周长/周长


谢谢

对于
L.Circle
可以从其半径计算周长:

L.Circle.include({
    circumference: function () {
        return 2 * Math.PI * this.getRadius();
    }
});

var circle = new L.Circle(...),
    circumference = circle.circumference();
对于
L.Polyline
,需要将
L.LatLng
对象之间的距离相加:

L.Polyline.include({
    length: function () {
        var latlngs = this.getLatLngs();
        var length = 0;
        for (var i = 0, n = latlngs.length - 1; i< n; i++) {
            length += latlngs[i].distanceTo(latlngs[i+1]);
        }
        return length;
    }
});

var polyline = new L.Polyline(...),
    length = polyline.length();
L.Polygon.include({
    circumference: function () {
        var length = L.Polyline.prototype.length.call(this);
        var latlngs = this.getLatLngs();
        if (latlngs.length > 2) {
            length += latlngs[0].distanceTo(latlngs[latlngs.length - 1]);
        }
        return length;
    }
});

var polygon = new L.Polygon(...),
    circumference = polygon.circumference();

谢谢你的回答和评论。正如@ghybs所建议的那样,这似乎是一条路要走。对于非线串的任何东西,它仍然需要一些编码,但至少算法是有效的。

也许有?您可以使用哈弗森公式计算点之间的距离,然后对每个距离求和。这是我在JS中的简单实现,不需要额外的库,我喜欢它!:-)顺便说一下,您可能是指
var length=L.Polyline…
中的
L.Polygon.include
Edited。感谢您发现@ghybs:)iH8的答案具有简单性的优点(特别是不需要转换为LineString),并且不需要额外的库。我觉得它是否适合您的需要是值得检查的。从GIS系统导出的数据非常复杂,没有简单的东西,都是多边形、多多边形和多线:-)