Reputation: 6888
How to show Locations those are within 2 miles from my current Location, i am using below code which allow me to get distance of few locations from my current location in Miles...
but now i want to know, which condition i need to write if i want my app to show locations within 2 miles ?
public void distanceBetweenTwoLocations() {
Location currentLocation = new Location("");
currentLocation.setLatitude(latitude);
currentLocation.setLongitude(longitude);
for (int i = 0; i < actorsList.size(); i++) {
locations = (Locations) actorsList.get(i);
Location destinationLocation = new Location(" ");
destinationLocation.setLatitude(Double.valueOf(actorsList.get(i).getLatitude()));
destinationLocation.setLongitude(Double.valueOf(actorsList.get(i).getLongitude()));
double inMeters = currentLocation.distanceTo(destinationLocation);
double inKms = inMeters / 1000;
double inMiles = inKms * 0.000621371;
DecimalFormat df = new DecimalFormat("#.##");
locations.setDistance(df.format(inMiles));
}
}
Upvotes: 0
Views: 297
Reputation: 6942
You need to compare your inMiles
object and need to set adapter again.
try with the following code :
public void distanceBetweenTwoLocations() {
Location currentLocation = new Location("");
currentLocation.setLatitude(latitude);
currentLocation.setLongitude(longitude);
ArrayList<Locations> myNewLocationList = new ArrayList<Locations>;
for (int i = 0; i < actorsList.size(); i++) {
locations = (Locations) actorsList.get(i);
Location destinationLocation = new Location(" ");
destinationLocation.setLatitude(Double.valueOf(actorsList.get(i).getLatitude()));
destinationLocation.setLongitude(Double.valueOf(actorsList.get(i).getLongitude()));
double inMeters = currentLocation.distanceTo(destinationLocation);
double inKms = inMeters / 1000;
double inMiles = inKms * 0.000621371;
DecimalFormat df = new DecimalFormat("#.##");
locations.setDistance(df.format(inMiles));
if (inMiles < 2) // Comapring Miles whether it is in 2 mile or not
myNewLocationList.add(mLocations); // adding it to new List
}
// Setting adapter again with new list having below 2 mile distance
adapter = new LocationsAdapter(getApplicationContext(), R.layout.adapter_locations, myNewLocationList);
listview.setAdapter(adapter);
}
Upvotes: 2