在Android中如何根据与当前位置的距离对地理点进行排序
我有一个“位置”对象,每个对象都有一个LatLng坐标:在Android中如何根据与当前位置的距离对地理点进行排序,android,geolocation,latitude-longitude,Android,Geolocation,Latitude Longitude,我有一个“位置”对象,每个对象都有一个LatLng坐标: import com.google.android.gms.maps.model.LatLng; public class Place{ public String name; public LatLng latlng; public Restaurant(String name, LatLng latlng) { this.name = name; this.latlng =
import com.google.android.gms.maps.model.LatLng;
public class Place{
public String name;
public LatLng latlng;
public Restaurant(String name, LatLng latlng) {
this.name = name;
this.latlng = latlng;
}
}
我有一个这些地方的列表,类似这样:
ArrayList<Place> places = new ArrayList<Place>();
places.add("Place 1", LatLng(90.0,90.0));
places.add("Place 2", LatLng(93.0,93.0));
places.add("Place 3", LatLng(83.0,92.0));
places.add("Place 4", LatLng(93.0,91.0));
如何根据最接近我的对象对这些对象进行排序?感谢您的帮助当您获得当前位置时,您可以通过计算行驶距离(最适合餐馆等场所)进行排序,如下所示:
http://maps.googleapis.com/maps/api/directions/json?origin="+yourLat+","+yourLong+"&destination="+toLat+","+toLong+"&sensor=false&mode=DRIVING
public class SortPlaces implements Comparator<Place> {
LatLng currentLoc;
public SortPlaces(LatLng current){
currentLoc = current;
}
@Override
public int compare(final Place place1, final Place place2) {
double lat1 = place1.latlng.latitude;
double lon1 = place1.latlng.longitude;
double lat2 = place2.latlng.latitude;
double lon2 = place2.latlng.longitude;
double distanceToPlace1 = distance(currentLoc.latitude, currentLoc.longitude, lat1, lon1);
double distanceToPlace2 = distance(currentLoc.latitude, currentLoc.longitude, lat2, lon2);
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;
}
}
以下是日志输出:
04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places before sorting﹕ Place: New York
04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places before sorting﹕ Place: Colorado
04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places before sorting﹕ Place: Los Angeles
04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places after sorting﹕ Place: Los Angeles
04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places after sorting﹕ Place: Colorado
04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places after sorting﹕ Place: New York
对于计算距离,有不同的方法可用。一个非常简单的公式是哈弗森公式()。更准确的计算方法是文森蒂公式。如果这两个位置不远,哈弗森溶液就足够了 计算完距离后,只需使用比较器对数组进行排序,如:
Collections.sort(places,newcomparator(){
公共整数比较(位置p1,位置p2){
返回Double.compare(p1.getDistance(),p2.getDistance());
}
});
您可以使用
distanceInMeters = (loc1.distanceTo(loc2));
从google maps API,然后将结果添加到树状图的键中您熟悉可比较的界面吗?我是,我用它来做一些简单的事情,比如比较整数,但是,我不知道如何比较距离之类的东西——首先需要计算距离。这应该很有用:你知道谷歌地图API是通过公路路线还是直线获得距离的吗?我想它提供的是公路路线而不是直线,如果是路线方向,我会检查一下,它将比直线更有用,再次感谢欢迎,您肯定需要道路路线,因为直线距离只是提供两个地理点之间的差异,这在现实世界中是不可用的。感谢您的帮助,我最后这样做了。如果你想用10万个位置来计算并按距离排序怎么办?@Beemo使用Google Play服务和distanceBetween或distanceTo在比较器中运行以获得最佳效率。如果你想用10万个位置来计算并按距离排序怎么办?@Beemo使用Google Play服务和distanceBetween或distanceTo距离在比较器中发挥作用以获得最佳效率。
04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places before sorting﹕ Place: New York
04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places before sorting﹕ Place: Colorado
04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places before sorting﹕ Place: Los Angeles
04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places after sorting﹕ Place: Los Angeles
04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places after sorting﹕ Place: Colorado
04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places after sorting﹕ Place: New York
distanceInMeters = (loc1.distanceTo(loc2));