【发布时间】:2016-08-25 06:56:39
【问题描述】:
所以我正在使用 Fabric API 开发一个失物招领的应用程序。它有一个选项可以根据用户的当前位置对收集的推文进行排序。我在网上找到了以下使用比较器进行排序的方法。但是,这似乎不起作用,并且排序前和排序后的结果完全一样。
public class SortLocations implements Comparator<Tweet> {
Double currLat;
Double currLng;
public SortLocations(Double currLat1, Double currLng1) {
currLat = currLat1;
currLng = currLng1;
}
@Override
public int compare(final Tweet tweet1, final Tweet tweet2) {
double lat1 = 0, lon1 = 0, lat2 = 0, lon2 = 0, distanceToPlace1 = 0, distanceToPlace2 = 0;
try {
lat1 = tweet1.coordinates.getLatitude();
lon1 = tweet1.coordinates.getLongitude();
lat2 = tweet2.coordinates.getLatitude();
lon2 = tweet2.coordinates.getLongitude();
distanceToPlace1 = distance(currLat, currLng, lat1, lon1);
distanceToPlace2 = distance(currLat, currLng, lat2, lon2);
} catch (Exception E) {
Log.d("No coordinates", "");
}
return (int) (distanceToPlace1 - distanceToPlace2);
}
public double distance(double fromLat, double fromLon, double toLat, double toLon) {
double radius = 6378137; // approximate Earth radius, *in meters*
double deltaLat = toLat - fromLat;
double deltaLon = toLon - fromLon;
double angle = 2 * Math.asin(Math.sqrt(
Math.pow(Math.sin(deltaLat / 2), 2) +
Math.cos(fromLat) * Math.cos(toLat) *
Math.pow(Math.sin(deltaLon / 2), 2)));
return radius * angle;
}
}
这是在我的活动中使用该类的方式
Collections.sort(tweetsSortedByLocation, new SortLocations(currLat, currLng));
其中 tweetsSortedByLocation 属于 List 类型。非常感谢任何帮助:)
【问题讨论】:
标签: android sorting twitter tweets twitter-fabric