Also ich entwickle eine verlorene und gefundene App mit Fabric API. Es besteht die Möglichkeit, die gesammelten Tweets basierend auf dem aktuellen Standort des Benutzers zu sortieren. Ich habe den unteren Weg online gefunden, um mit einem Komparator zu sortieren. Dies scheint jedoch nicht zu funktionieren und die Vorsortier- und Nachsortierergebnisse sind völlig gleich.Twitter Fabric API: Sortierung Tweets basierend auf Standort (am nächsten)
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;
}
}
Dies ist, wie die Klasse in meiner Tätigkeit verwendet wird
Collections.sort(tweetsSortedByLocation, new SortLocations(currLat, currLng));
Wo tweetsSortedByLocation vom Typ Liste ist. Jede Hilfe wird wirklich geschätzt :)
Vielen Dank für die Antwort Zach !. Schätzen Sie Ihre umfassende Lösung. –