如何在Leaflet中计算折线的距离,类似于geojson.io?

11

我正在使用Mapbox和Leaflet制作地图,用户可以绘制多边形并计算并显示多边形的面积,还可以绘制折线并显示折线的距离。

我已经解决了多边形面积的问题,但是我不知道如何计算折线的距离。

我的代码如下:

loadScript('https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-draw/v0.2.2/leaflet.draw.js', function(){
    loadScript('https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-geodesy/v0.1.0/leaflet-geodesy.js', function(){
        var featureGroup = L.featureGroup().addTo(map);

        var drawControl = new L.Control.Draw({
        edit: {
            featureGroup: featureGroup
        },
        draw: {
            polygon: true,
            polyline: true,
            rectangle: false,
            circle: false,
            marker: false
        }
    }).addTo(map);

    map.on('draw:created', showPolygonArea);
    map.on('draw:edited', showPolygonAreaEdited);

    function showPolygonAreaEdited(e) {
        e.layers.eachLayer(function(layer) {
            showPolygonArea({ layer: layer });
        });
    }
    function showPolygonArea(e) {
        var type = e.layerType,
        layer = e.layer;

        if (type === 'polygon') {
            featureGroup.clearLayers();
            featureGroup.addLayer(e.layer);
            e.layer.bindPopup(((LGeo.area(e.layer) / 1000000) * 0.62137).toFixed(2) + ' mi<sup>2</sup>');
            e.layer.openPopup();
        }

        if (type === 'polyline') {
            featureGroup.clearLayers();
            featureGroup.addLayer(e.layer);
            // What do I do different here to calculate the distance of the polyline?
            // Is there a method in the LGeo lib itself?
            // e.layer.bindPopup(((LGeo.area(e.layer) / 1000000) * 0.62137).toFixed(2) + ' mi<sup>2</sup>');
            e.layer.openPopup();
        }

    }
    });
});

LGeo库本身是否有一个方法可以帮助我计算折线的距离?在geogson.io上的开发人员也有一种计算距离的方法,但是通过查看他们的代码似乎无法弄清楚。我不是经验丰富的JavaScript开发人员。欢迎任何帮助。 :)

4个回答

20

所以最终我自己想出了一个算法。我基本上找到了折线的属性,该属性保存了折线的所有latlngs,然后我让它通过一个循环,并使用Leaflet中的distanceTo方法计算点之间的距离,不断将它们添加到一个totalDistance变量中。

if (type === 'polyline') {
    featureGroup.clearLayers();
    featureGroup.addLayer(e.layer);

    // Calculating the distance of the polyline
    var tempLatLng = null;
    var totalDistance = 0.00000;
    $.each(e.layer._latlngs, function(i, latlng){
        if(tempLatLng == null){
            tempLatLng = latlng;
            return;
        }

        totalDistance += tempLatLng.distanceTo(latlng);
        tempLatLng = latlng;
    });
    e.layer.bindPopup((totalDistance).toFixed(2) + ' meters');
    e.layer.openPopup();
}

太棒了,完美的解决方案!那么如何计算圆的面积呢?我怎样才能获取圆的半径(已创建或编辑)? - serifsadi

15

我通过扩展L.Polyline类,并使用LatLngdistanceTo方法来解决这个问题:

L.Polyline = L.Polyline.include({
    getDistance: function(system) {
        // distance in meters
        var mDistanse = 0,
            length = this._latlngs.length;
        for (var i = 1; i < length; i++) {
            mDistanse += this._latlngs[i].distanceTo(this._latlngs[i - 1]);
        }
        // optional
        if (system === 'imperial') {
            return mDistanse / 1609.34;
        } else {
            return mDistanse / 1000;
        }
    }
});

希望能对某些人有所帮助。


1
在将 .extend 更改为 .include 后,这对我起作用了。 - user2738183
1
@JillRussek 没错,谢谢!我已经提交了一份编辑建议。 - frzsombor

4
而且这就是计算圆面积的解决方案。
else if (type === 'circle') {
     var area = 0;
     var radius = e.layer.getRadius();
     area = (Math.PI) * (radius * radius);
     e.layer.bindPopup((area / 1000000).toFixed(2) + ' km<sup>2</sup>');
     e.layer.openPopup();
}

2

我也鼓励读者查看turf库。它与leaflet搭配使用效果很好,并且具有许多有用的方法,包括折线距离。 http://turfjs.org/docs


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接