如何显示距离我当前位置2英里范围内的位置,我使用下面的代码,这样我就可以从迈尔斯当前位置获得几个位置的距离......
但现在我想知道,如果我希望我的应用在2英里内显示位置,我需要写哪个条件?
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));
}
}
答案 0 :(得分:2)
您需要比较inMiles
对象,并需要再次设置适配器。
尝试使用以下代码:
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);
}