我想要做的是:用户选择地图上的开始和目的地,然后从他们的坐标中我想显示地图上的位置列表中的最近点位置。我有一个简单的Sqlite数据库,其中包含可能位置的经度,纬度和名称。找到最近的Gps指向用户位置形成一个列表
我做了一些研究,这是我发现:
http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL
,但这是为使用它与MySQL和某种空间搜索扩展。 有没有可能使用android api或external libs做类似的事情?
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
哪里是包含一个对象:纬度,经度,距离,名称
然而这种解决方案不提供正确的回报信息......我意识到这是不可扩展的,而且非常缓慢。我需要一个解决方案,该解决方案可以用最多1000行的数据库快速执行。
编辑:我有这个代码现在分拣错误我把它换成(应该是<代替>)
我相信你已经通过PHP传递变量。 $ lat是您当前的纬度,$ lon是当前的纬度。但是什么是$ R? – 2012-12-16 19:54:00
不是$ R地球半径?请参阅:http://www.movable-type.co.uk/scripts/latlong.html – leochab 2013-07-05 07:08:15