mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Print distance between current location and Place
This commit is contained in:
parent
8adb257e97
commit
a0ea2a09b9
1 changed files with 26 additions and 7 deletions
|
|
@ -17,6 +17,7 @@ import android.widget.ListView;
|
||||||
import android.widget.ProgressBar;
|
import android.widget.ProgressBar;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import java.text.NumberFormat;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -25,12 +26,12 @@ import fr.free.nrw.commons.R;
|
||||||
|
|
||||||
public class NearbyListFragment extends ListFragment {
|
public class NearbyListFragment extends ListFragment {
|
||||||
|
|
||||||
private LatLng mLatestLocation;
|
|
||||||
private int mImageSize;
|
private int mImageSize;
|
||||||
private boolean mItemClicked;
|
private boolean mItemClicked;
|
||||||
ArrayAdapter mAdapter;
|
private ArrayAdapter mAdapter;
|
||||||
|
|
||||||
List<Place> places;
|
private List<Place> places;
|
||||||
|
private LatLng mLatestLocation;
|
||||||
|
|
||||||
private static final String TAG = "NearbyListFragment";
|
private static final String TAG = "NearbyListFragment";
|
||||||
|
|
||||||
|
|
@ -67,9 +68,9 @@ public class NearbyListFragment extends ListFragment {
|
||||||
|
|
||||||
//Load from data source (NearbyPlaces.java)
|
//Load from data source (NearbyPlaces.java)
|
||||||
|
|
||||||
//LatLng mLatestLocation = ((NearbyActivity) getActivity()).getmLatestLocation();
|
mLatestLocation = ((NearbyActivity) getActivity()).getmLatestLocation();
|
||||||
//FIXME: Hardcoding mLatestLocation to Michigan for testing
|
//FIXME: Hardcoding mLatestLocation to Michigan for testing
|
||||||
LatLng mLatestLocation = new LatLng(44.182205, -84.506836);
|
mLatestLocation = new LatLng(44.182205, -84.506836);
|
||||||
places = loadAttractionsFromLocation(mLatestLocation);
|
places = loadAttractionsFromLocation(mLatestLocation);
|
||||||
|
|
||||||
final ListView listview = (ListView) view.findViewById(R.id.listview);
|
final ListView listview = (ListView) view.findViewById(R.id.listview);
|
||||||
|
|
@ -81,7 +82,7 @@ public class NearbyListFragment extends ListFragment {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static List<Place> loadAttractionsFromLocation(final LatLng curLatLng) {
|
private List<Place> loadAttractionsFromLocation(final LatLng curLatLng) {
|
||||||
|
|
||||||
List<Place> places = NearbyPlaces.get();
|
List<Place> places = NearbyPlaces.get();
|
||||||
if (curLatLng != null) {
|
if (curLatLng != null) {
|
||||||
|
|
@ -101,11 +102,29 @@ public class NearbyListFragment extends ListFragment {
|
||||||
}
|
}
|
||||||
//FIXME: This doesn't sort appropriately
|
//FIXME: This doesn't sort appropriately
|
||||||
for(int i = 0; i < places.size(); i++) {
|
for(int i = 0; i < places.size(); i++) {
|
||||||
System.out.println("Sorted " + places.get(i).name);
|
String distance = formatDistanceBetween(mLatestLocation, places.get(i).location);
|
||||||
|
System.out.println("Sorted " + places.get(i).name + " at " + distance + " away.");
|
||||||
}
|
}
|
||||||
return places;
|
return places;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String formatDistanceBetween(LatLng point1, LatLng point2) {
|
||||||
|
if (point1 == null || point2 == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
NumberFormat numberFormat = NumberFormat.getNumberInstance();
|
||||||
|
double distance = Math.round(computeDistanceBetween(point1, point2));
|
||||||
|
|
||||||
|
// Adjust to KM if M goes over 1000 (see javadoc of method for note
|
||||||
|
// on only supporting metric)
|
||||||
|
if (distance >= 1000) {
|
||||||
|
numberFormat.setMaximumFractionDigits(1);
|
||||||
|
return numberFormat.format(distance / 1000) + "km";
|
||||||
|
}
|
||||||
|
return numberFormat.format(distance) + "m";
|
||||||
|
}
|
||||||
|
|
||||||
private static double computeDistanceBetween(LatLng from, LatLng to) {
|
private static double computeDistanceBetween(LatLng from, LatLng to) {
|
||||||
return computeAngleBetween(from, to) * 6371009.0D;
|
return computeAngleBetween(from, to) * 6371009.0D;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue