查找最近全球定位系统指向用户位置形成一个列表 [英] find nearest Gps point to the user location form a list

查看:141
本文介绍了查找最近全球定位系统指向用户位置形成一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的事:用户选择,并开始在地图上目的地,然后从他们的坐标,我想说明从在地图上的位置列表中的最近点的位置。我有一个包含的可能位置的经度,纬度和名称的简单的SQLite数据库。

what i am trying to do: the user selects start and destination on a map and then from their coordinates i want to show the closest point location from a list of locations on map. i have a simple Sqlite database containing the longitude,latitude and name of the possible locations.

我做了一些研究,这是我发现了什么:

i did some research and this is what i found:

http://www.scribd.com/doc/ 2569355 /地理距离 - 搜索 - 与MySQL的

但这是为使用它与MySQL和某种空间搜索推广的。 是否有可能,我可以做同样的事情使用的Andr​​oid API或外部库?

but this is meant for using it with mySql and some kind of spatial search extension. is there a possibility i can do something similar using android api or 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();
}

这是在code在PointComparator()类:

this is the code in the PointComparator() class:

   public int compare(PointWithDistance o1, PointWithDistance o2) {
  return (o1.getDistance()<o2.getDistance() ? -1 : (o1.getDistance()==o2.getDistance() ? 0 : 1));
 }

其中, PointWithDistance 是包含一个对象:土地增值税,长,距离远,名称

where PointWithDistance is a object that contains: lat, long , distance, name

但是这种解决方案并没有提供正确的返回信息...我意识到,这不正是可扩展性都非常缓慢。我需要将执行速度快与最高的1000行的数据库解决方案。

however this solution doesn't provide the right return info... and i realize that is it not scalable at all and very slow. i need a solution that will execute fast with a database with max of 1000 rows.

编辑:我有这个code在分类的错误,现在我把它改变了(应该是&LT;代替>)

edit: my there was a mistake in this code in the sorting now i have it changed( should be < instead of >)

推荐答案

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

I was looking for something very similar some time ago:

<一个href="http://stackoverflow.com/questions/2034348/android-sqlite-sort-on-calculated-column-co-ordinates-distance">http://stackoverflow.com/questions/2034348/android-sqlite-sort-on-calculated-column-co-ordinates-distance

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

I was using a MySQL lookup on my server, MySQL allows you to create a virtual column, performs the calculation and sorts by distance, and then you can set the max results returned or the max distance - it works very well:

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

我想执行相同的操作在我的应用程序 - 为了从用户的位置,让我展现最接近的人拉开距离拉所有的点。我结束了沿之一的线的解决方案建议去上面的链接,但实现它的可能不是最佳的解决方案,但适用于我想要的目的。

I wanted to perform the same operation in my app - pull all the points in order to distance from the users location allowing me to show the closest ones. I ended up going with the a solution along the lines of the one suggested on the link above but realise its probably not the optimal solution but works for the purpose I wanted.

这篇关于查找最近全球定位系统指向用户位置形成一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆