所以我正在开发一个使用Fabric API的丢失和找到的应用程序。它可以根据用户的当前位置对收集的推文进行排序。我发现在线方式使用比较器进行排序。然而,这似乎并不奏效,而前分类和后分类结果完全相同。Twitter的织物API:根据位置排序tweets(最近的第一个)
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使用。任何帮助真的很感激:)
感谢您的答复!扎克非常感谢您的全面解决方案。 –