经度\纬度点的排序名单,先从最近 [英] Sort list of lon\lat points, start with nearest
问题描述
我从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_base \ lat_base1英里? 2.如何对它们进行排序,从最接近最远?
My questions are: 1. How do I get from that list only the lon\lat that are 1 mile from my lon_base\lat_base? 2. How do I sort them from closest to farthest?
在此先感谢!
推荐答案
您需要定义自己的比较
,在一般情况下,看起来是这样的:
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上,我认为谷歌地图具有这样的功能的地方在其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.
修改:你希望你的 calcDistance()
函数看起来像ChrisJ的距离
功能。
EDIT : You'll want your calcDistance()
function to look like ChrisJ's distance
function.
-tjw
这篇关于经度\纬度点的排序名单,先从最近的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!