如何根据Android中当前位置的距离对地理点进行排序 [英] How to Sort Geo-points according to the distance from current location in Android
问题描述
我有一个带有LatLng坐标的Place对象:
import com.google.android.gms。 maps.model.LatLng;
public class Place {
public String name;
public LatLng latlng;
public Restaurant(字符串名称,LatLng latlng){
this.name = name;
this.latlng = latlng;
$ / code $ / pre
我有这些位置的ArrayList,类似于这:
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));
我有我的LatLng:
LatLng myLocation = new LatLng(10.0,10.0);
如何根据最接近我的方式对这些对象进行排序?感谢您的帮助
解决方案从。 .wikipedia.org / wiki / Great-circle_distancerel =nofollow noreferrer> Great Circle Distance ,我得到了这个例子。
这是比较器:
public class SortPlaces实现了Comparator< Place> {
LatLng currentLoc;
public SortPlaces(LatLng current){
currentLoc = current;
}
@Override
public int compare(final Place1,final 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);
}
公开双倍距离(双重fromLat,double fromLon,double toLat,double toLon){
double radius = 6378137; //近似地球半径,*以米为单位*
双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)));
返回半径*角度;
$ / code $ / pre
这是高级代码,我只是把这个在 onCreate()
:
//我的位置,旧金山
双lat = 37.77657;
double lng = -122.417506;
LatLng latLng =新LatLng(lat,lng);
//设置列表
ArrayList< Place> places = new ArrayList< Place>();
places.add(new Place(New York,new LatLng(40.571256,73.98369)));
places.add(new Place(Colorado,new LatLng(39.260658,-105.101615)));
places.add(new Place(Los Angeles,new LatLng(33.986816,118.473819))); (Place p:places){
Log.i(排序前的位置,Place:+ p.name);
}
//对列表进行排序,给比较器当前位置
Collections.sort(places,new SortPlaces(latLng)); (Place p:places){
Log.i(排序后的位置,Place:+ p.name);
}
以下是日志输出:
04-17 23:04:16.074 12963-12963 / com.maptest.daniel.maptest排序前的我/地点:地点:纽约
04-17 23:04:16.074 12963-12963 / com.maptest.daniel.maptest排序前的I /地点:地点:Colorado
04-17 23:04:16.074 12963-12963 / com.maptest.daniel.maptest I /排序前的地点:地点:洛杉矶
04-17 23:04:16.074 12963-12963 / com.maptest.daniel.maptest排序后的我/地点:地点:洛杉矶
04-17 23: 04:16.074 12963-12963 / com.maptest.daniel.maptest排序后的I /地点:地点:科罗拉多州
04-17 23:04:16.074 12963-12963 / com.maptest.daniel.maptest我/之后的地点排序:地点:纽约
I have a "Place" object with a LatLng coordinate for each:
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;
}
}
and I have an ArrayList of these Places, something like this:
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));
and I have "my" LatLng:
LatLng myLocation = new LatLng(10.0,10.0);
How can I sort these objects according to closest to me? Thanks for the help
解决方案 Taking the algorithm from this answer from the question posted by @shieldstroy, that uses the Great Circle Distance, I got this example working.
Here is the Comparator
:
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;
}
}
Here is the high level code, I just put this in onCreate()
:
//My location, San Francisco
double lat = 37.77657;
double lng = -122.417506;
LatLng latLng = new LatLng(lat, lng);
//set up list
ArrayList<Place> places = new ArrayList<Place>();
places.add(new Place("New York", new LatLng(40.571256,73.98369)));
places.add(new Place("Colorado", new LatLng(39.260658,-105.101615)));
places.add(new Place("Los Angeles", new LatLng(33.986816,118.473819)));
for (Place p: places){
Log.i("Places before sorting", "Place: " + p.name);
}
//sort the list, give the Comparator the current location
Collections.sort(places, new SortPlaces(latLng));
for (Place p: places){
Log.i("Places after sorting", "Place: " + p.name);
}
Here is the log output:
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
这篇关于如何根据Android中当前位置的距离对地理点进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!