问题描述
我有来自 GPS 的位置(lon_base、lat_base).我有一个位置列表(lon1、lat1|lon2、lat2|lon3、lat3...)这个列表很长,遍布世界各地.
I have location from GPS (lon_base, lat_base). I have a list of locations (lon1, lat1|lon2, lat2|lon3, lat3...) This list is very long and is around the world.
我的问题是:1. 如何从该列表中仅获取距离我的 lon_baselat_base 1 英里的 lonlat?2. 如何从近到远排序?
My questions are: 1. How do I get from that list only the lonlat that are 1 mile from my lon_baselat_base? 2. How do I sort them from closest to farthest?
提前致谢!
推荐答案
你想定义你自己的 Comparator
,一般看起来像这样:
You want to define your own Comparator
that, in general, looks something like this:
LonLat myHouse = /* whatever */ ;
Comparable comp = new Comparable () {
LonLat a;
int compareTo (Object b) {
int aDist = calcDistance(a, myHouse) ;
int bDist = calcDistance(b, myHouse) ;
return aDist - bDist;
}
};
myLonLatList.sort(lonLatList, comp);
其中 calcDistance()
只是计算两点之间的距离.如果您使用的是 Android,我认为 Google Maps 在其 API 中的某处有一个功能可以为您执行此操作.
where calcDistance()
simply calculates the distance between the two points. If you're on Android, I think Google Maps has a function somewhere in their API that will do this for you.
EDIT:你会希望你的 calcDistance()
函数看起来像 ChrisJ 的 distance
函数.
EDIT : You'll want your calcDistance()
function to look like ChrisJ's distance
function.
-tjw
这篇关于对 lonlat 点的排序列表,从最近的开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!