我想做的是:用户在 map 上选择起点和终点,然后根据他们的坐标,我想从 map 上的位置列表中显示最近的点位置。我有一个简单的 Sqlite 数据库,其中包含可能位置的经度、纬度和名称。
我做了一些研究,这是我发现的:
http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL
但这是为了将它与 mySql 和某种空间搜索扩展一起使用。 有没有可能我可以使用 android api 或外部库做类似的事情?
public Point dialogFindClosestLocationToPoint(geometry.Point aStartPoint){
List<PointWithDistance> helperList=new ArrayList<PointWithDistance>();
try {
openDataBase();
Cursor c=getCursorQueryWithAllTheData();
if(c.moveToFirst())
do{
PointWithDistance helper=new PointWithDistance(c.getDouble(1),c.getDouble(2),c.getString(3));
int distance=returnDistanceBetween2Points(aStartPoint, helper);
if(distance<MAX_SEARCH_DISTANCE){
helper.setDistance(distance);
Log.i("values", helper.name);
helperList.add(helper);
}
}while (c.moveToNext());
Collections.sort(helperList,new PointComparator());
if(helperList!=null)
return helperList.get(0);
else return null;
}catch(SQLException sqle){
throw sqle;
}
finally{
close();
}
这是 PointComparator() 类中的代码:
public int compare(PointWithDistance o1, PointWithDistance o2) {
return (o1.getDistance()<o2.getDistance() ? -1 : (o1.getDistance()==o2.getDistance() ? 0 : 1));
}
其中 PointWithDistance
是一个包含:lat, long, distance, name 的对象
但是这个解决方案没有提供正确的返回信息......我意识到它根本不可扩展而且非常慢。我需要一个能够在最多 1000 行的数据库中快速执行的解决方案。
编辑:我的排序代码中有一个错误,现在我已经更改了它(应该是 < 而不是 >)
最佳答案
使用 R-Tree 可以最有效地完成这种事情. JSI library提供了一个 Java 实现,我已经成功地使用了 80.000 个位置的索引,每秒处理数千次查找。但是,它可能无法在 Android 上运行。
关于java - 从列表中找到离用户位置最近的 Gps 点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3987336/