android - 对 lon\lat 点列表进行排序,从最近的开始

标签 android sorting gps distance point

我有来自 GPS 的位置(lon_base,lat_base)。 我有一个位置列表(lon1、lat1|lon2、lat2|lon3、lat3...) 这个名单很长,遍布世界各地。

我的问题是: 1. 如何从该列表中仅获取距我的 lon_base\lat_base 1 英里的 lon\lat? 2. 如何将它们从近到远排序?

提前致谢!

最佳答案

public static List<Location> sortLocations(List<Location> locations, final double myLatitude,final double myLongitude) {
    Comparator comp = new Comparator<Location>() {
        @Override
        public int compare(Location o, Location o2) {
            float[] result1 = new float[3];
            android.location.Location.distanceBetween(myLatitude, myLongitude, o.Lat, o.Long, result1);
            Float distance1 = result1[0];

            float[] result2 = new float[3];
            android.location.Location.distanceBetween(myLatitude, myLongitude, o2.Lat, o2.Long, result2);
            Float distance2 = result2[0];

            return distance1.compareTo(distance2);
        }
    };


    Collections.sort(locations, comp);
    return locations;
}

位置列表是包含您自己的位置类的列表,而不是 android.location.Location。

关于android - 对 lon\lat 点列表进行排序,从最近的开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5396286/

相关文章:

android - 在 OnTouch 中返回 true 和 false?

Android AvoidXferMode 绘图路径错误?

安卓 : best case for communicating adapter with Activity/Fragment

android - Ionic - 启用 GPS

delphi - 在lazarus中安装CCR.EXIF包

android - 使用 Mockito 模拟改造服务导致 ExceptionInInitializerError

javascript - 使用多个键对数组对象进行排序 : Javascript

java - 我的 java 数组代码表现得很有趣

java - 试图在数组中找到最小值

c# - 在并发环境中更新 Bing Map map