use*_*637 6 android geolocation latitude-longitude
我有一个"Place"对象,每个对象都有一个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;
}
}
Run Code Online (Sandbox Code Playgroud)
我有这些地方的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));
Run Code Online (Sandbox Code Playgroud)
我有"我的"LatLng:
LatLng myLocation = new LatLng(10.0,10.0);
Run Code Online (Sandbox Code Playgroud)
如何根据离我最近的方式对这些物体进行排序?谢谢您的帮助
Dan*_*ent 10
从@shieldstroy发布的使用Great Circle Distance的问题中得出这个答案的算法,我得到了这个例子.
这是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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是高级代码,我把它放在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);
}
Run Code Online (Sandbox Code Playgroud)
这是日志输出:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11288 次 |
| 最近记录: |