对象内对象的交叉比较

时间:2018-04-30 12:55:07

标签: javascript arrays object

我想交叉比较包含位置数据(纬度,经度)的多个对象,以获得它们之间的距离。目标是从一堆位置计算最远的两个位置。我已经知道如何计算两个地点之间的距离,但如果你有多个地点该怎么办?

function FarthestDistance(points) {
// here this function should cross compare all objects within 'points' to calculate which two locations have the furthest distance between them
// the function should calculate the distance between each point in a recursive manner and return the two farthest points back
}

var obj = {{lat1,lon1},{lat2,lon2},{lat3,lon3},{lat4,lon4}};
FarthestDistance(obj);

希望现在很清楚。感谢。

1 个答案:

答案 0 :(得分:0)

好吧,这是我正在制作的Magic Mirror模块。它是Traccar Service在魔镜上部署的包装器。我需要从包含每个注册用户的多个位置的对象计算地图上最远的两个点。然后,一旦我在地图上有最远的两个点,我就可以计算它们之间的中间点,将其设置为地图的中心。现在我正在研究地图的缩放以包括地图上的所有标记..无论如何,对于我在这里解释的问题,解决方案是经过一些研究后发现的。在这里。

function toRadians (deg){ // Helper function
    return deg * (Math.PI/180);
}

function toDegrees (rad){ // Helper function
    return rad * (180/Math.PI);
}

function distance (obj){ // Helper function from https://www.movable-type.co.uk/scripts/latlong.html | http://mathforum.org/library/drmath/view/51822.html
    var R = 6371e3; // metres
    var φ1 = obj[0][0];
    var φ2 = obj[1][0];
    var Δφ = obj[1][0]-obj[0][0];
    var Δλ = obj[1][1]-obj[0][1];

    var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
            Math.cos(φ1) * Math.cos(φ2) *
            Math.sin(Δλ/2) * Math.sin(Δλ/2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    return (R * c);
}

function midPoints(obj){ // Helper functions (which I modified for the specifications) from https://stackoverflow.com/questions/477591/algorithm-to-find-two-points-furthest-away-from-each-other | https://www.movable-type.co.uk/scripts/latlong.html | http://mathforum.org/library/drmath/view/51822.html
    var self = this;
    var solution = {"start": [], "end": [], "distance": 0};
    for (i = 0; i < obj.length; i++) { 
        for (j = i+1; j < obj.length; j++) {
            var newpoint = [
                [
                    self.toRadians(obj[i][0]),
                    self.toRadians(obj[i][1])
                ],
                [
                    self.toRadians(obj[j][0]),
                    self.toRadians(obj[j][1])
                ]
            ];
            var distance = self.distance(newpoint);
            if (distance > solution.distance){
                solution = {"start": [obj[i][0],obj[i][1]], "end": [obj[j][0],obj[j][1]], "distance": distance}
            }
        }
    }
    var Bx = Math.cos(solution.end[0]) * Math.cos(solution.end[1]-solution.start[1]);
    var By = Math.cos(solution.end[0]) * Math.sin(solution.end[1]-solution.start[1]);
    var latMid = Math.atan2(Math.sin(solution.start[0]) + Math.sin(solution.end[0]),Math.sqrt((Math.cos(solution.start[0])+Bx)*(Math.cos(solution.start[0])+Bx) + By*By ) );
    var lonMid = solution.start[1] + Math.atan2(By, Math.cos(solution.start[0]) + Bx);
    return {"lat": self.toDegrees(latMid), "lon": self.toDegrees(lonMid), "distance": solution.distance};
}