Javascript 以米为单位计算openlayers 3中两点之间的距离

Javascript 以米为单位计算openlayers 3中两点之间的距离,javascript,distance,openlayers-3,Javascript,Distance,Openlayers 3,我想在openlayers 3中以足够的精度获得两点之间的距离 我将其实现为以下功能: function getCoordsDistance(firstPoint, secondPoint, projection) { projection = projection || 'EPSG:4326'; length = 0; var sourceProj = mapObj.getView().getProjection(); var c1 = ol.proj.tra

我想在openlayers 3中以足够的精度获得两点之间的距离

我将其实现为以下功能:

function getCoordsDistance(firstPoint, secondPoint, projection) {
    projection = projection || 'EPSG:4326';

    length = 0;
    var sourceProj = mapObj.getView().getProjection();
    var c1 = ol.proj.transform(firstPoint, sourceProj, projection);
    var c2 = ol.proj.transform(secondPoint, sourceProj, projection);

    var wgs84Sphere = new ol.Sphere(6378137);
    length += wgs84Sphere.haversineDistance(c1, c2);

    return length;
}

可能重复@NagaveerGowda的可能重复问题仅限于openlayers 3。我专门为openlayers3实现它。所以请在投票前观看。