【发布时间】:2010-10-21 12:20:19
【问题描述】:
我想要做什么:用户在地图上选择起点和目的地,然后从他们的坐标中我想从地图上的位置列表中显示最近的点位置。我有一个简单的 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 行的数据库中快速执行。
编辑:我在排序中此代码有错误,现在我已更改(应该是 )
【问题讨论】:
标签: java android database location