【问题标题】:find nearest Gps point to the user location form a list从列表中查找到用户位置最近的 Gps 点
【发布时间】: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


    【解决方案1】:

    使用R-Tree 可以最有效地完成这种事情。 JSI library 提供了一个 Java 实现,我已经成功地使用了它,它具有 80.000 个位置的索引,每秒处理数千次查找。但是,它可能无法在 Android 上运行。

    【讨论】:

    • 是的,我发现一些信息表明 r-tree 是一个很好的解决方案,但到目前为止我还没有找到任何适用于 android 的库。我会继续寻找并尝试上述方法。
    【解决方案2】:

    前段时间我在寻找非常相似的东西:

    Android sqlite sort on calculated column (co-ordinates distance)

    我在我的服务器上使用 MySQL 查找,MySQL 允许您创建一个虚拟列,执行计算并按距离排序,然后您可以设置返回的最大结果或最大距离 - 它工作得很好:

    Select Lat, Lon, acos(sin($lat)*sin(radians(Lat)) + cos($lat)*cos(radians(Lat))cos(radians(Lon)-$lon))$R As dist From MyTable ORDER BY dist DESC
    

    我想在我的应用程序中执行相同的操作 - 拉出所有点以便与用户位置保持距离,以便我显示最近的点。我最终采用了上面链接中建议的解决方案,但意识到它可能不是最佳解决方案,但可以达到我想要的目的。

    【讨论】:

    • 我相信你一直在通过 php.ini 传递变量。 $lat 是您当前的纬度,$lon 是当前纬度。但是 $R 是什么?
    • $R 不是地球半径吗?见:movable-type.co.uk/scripts/latlong.html
    【解决方案3】:

    我没有尝试运行你的代码,但它似乎可以工作,只是效率不高。就像您实际上不需要排序一样,您需要提取最小值。

    您可以将查询限制为大小为 (2*MAX_SEARCH_DISTANCE)^2 的正方形(您的点位于中间。 这样您就可以本地化您的查询,这将返回更少的结果来计算距离。 当然,如果您的所有位置都在本地化的方格内(可能不太可能?),这将无济于事。

    另外,我想您可以使用汉密尔顿距离而不是欧几里得距离。 欧式距离 = sqrt((lat0 - lat1)^2 + (lon0 - lon1)^2) 哈密​​顿距离 = (lat0 - lat1) + (lon0 - lon1)

    【讨论】:

    • 我实际上是使用Android Location api中定义的函数来计算两个gps坐标之间的距离。我发现它非常精确,并且还考虑了地球的形状。 “你需要最少的提取物。”最好的方法是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多