我已经回顾了几个类似于我的问题,但没有运气。从我所看到的我需要创建一个可比的,但没有任何成功。这就是我现在所拥有的。
我能够成功获得2个可以提供当前位置的双打。一个用于纬度,一个用于经度。
double myCurrentLong;
double myCurrentLat;
我需要排序的列表如下。
List<Locations> myLocations = locationLibrary.getLocations();
每个“myLocations”都包含一个纬度和经度。为了清楚起见我得到了正确的数字,但在尝试对它们进行排序时遇到了问题。
感谢所有帮助。感谢。
下面是我尝试通过首先获得距离来解决问题的尝试。为了测试,我在那里使用虚拟数据。
/*private double getDistance(){
Location locationA = new Location("point A");
locationA.setLatitude(20.45);
locationA.setLongitude(30.45);
Location locationB = new Location("point B");
locationB.setLatitude(GeoCacheLibrary.get(getActivity()).getLatitude());
locationB.setLongitude(GeoCacheLibrary.get(getActivity()).getLongitude());
float distance = locationA.distanceTo(locationB);
return distance;
}*/
答案 0 :(得分:2)
我没有您的课程或已安装的Android SDK,因此请随时编辑我的帖子。基本思路就像是
double myCurrentLong;
double myCurrentLat;
//Fill those two variables somehow here
Location myCurrentLoc = new Location("My Current");
locationA.setLongitude(myCurrentLong);
myCurrentLoc.setLatitude(myCurrentLat);
List<Locations> myLocations = locationLibrary.getLocations();
Collections.sort(ls, new Comparator<Location>(){
public int compare(Location l1, Location l2) {
return l1.distanceTo(myCurrentLoc) - l2.distanceTo(myCurrentLoc);
}
});
基本思路是使用匿名比较器中的当前位置,使用计算出的距离值比较列表中的对象。
答案 1 :(得分:0)
问题是你试图对位置进行排序,但是距离是两个位置之间的差异。 通过这种方式,U可以轻松排序距离,但不能排序位置(从哪个位置到另一个位置)。 你可以试试自己的班级即。路径(位置A,位置B)和列表u应该可以轻松按距离排序(比较方法中的计数距离),只需在集合上调用排序方法。