如何显示 2 英里内的位置?
Posted
技术标签:
【中文标题】如何显示 2 英里内的位置?【英文标题】:How to show Locations those are within 2 Miles? 【发布时间】:2014-08-11 06:38:47 【问题描述】:如何显示距离我当前位置 2 英里范围内的位置,我正在使用以下代码,它允许我从我当前在 Miles 的位置获取几个位置的距离...
但现在我想知道,如果我想让我的应用程序显示 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));
【问题讨论】:
【参考方案1】:您需要比较您的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);
【讨论】:
这么好的解决方案 :) 一个伟大的程序员以上是关于如何显示 2 英里内的位置?的主要内容,如果未能解决你的问题,请参考以下文章