如何排序的地理点的数组列表基于最近的距离在android地图



我有一个具有lat/lng值的客户位置数组列表,我需要从我的位置出发,从最近的客户出发,然后根据最近的客户出发。如何排序的数组地理点基于他们的距离?

根据下面的代码片段创建Java比较器,然后使用相同的方法对ArrayList进行排序。

获取两个地理位置之间距离的示例代码

private double distanceBetween(final GeoLocation from, final GeoLocation to) {
    // double earthRadius = 3958.75; // in miles, change to 6371 for
    // kilometer output
    final double earthRadius = 6371;
    final double dLat = Math.toRadians(to.lattitude - from.lattitude);
    final double dLng = Math.toRadians(to.longitude - from.longitude);
    final double sindLat = Math.sin(dLat / 2);
    final double sindLng = Math.sin(dLng / 2);
    final double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2) * Math.cos(Math.toRadians(from.lattitude))
        * Math.cos(Math.toRadians(to.lattitude));
    final double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    final double dist = earthRadius * c;
    return dist;
}

使用Google的距离矩阵API。

在环路内,根据传输方式使用距离矩阵API来获得到达它们的距离和持续时间。

根据持续时间,使用任意排序技术对数组列表进行排序。

如果你是在飞行,直接计算时差和时差之间的距离就可以了。

最新更新