things i am attempting to do: the consumer chooses start and destination on the map after which using their coordinates i wish to show the nearest point location from a listing of locations on map. i've got a simple Sqlite database that contains the longitude,latitude and title from the possible locations.

used to do some investigation which is things i found:

http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL

but this really is intended for utilizing it with mySql plus some type of spatial search extension. it is possible to possibility i'm able to make a move similar using android api or exterior 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();
}

this is actually the code within the PointComparator() class:

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

where PointWithDistance is really a object that consists of: lat, lengthy , distance, title

however this solution does not supply the right return info... and that i realize that's it not scalable whatsoever and incredibly slow. i want an answer which will execute fast having a database with max of 1000 rows.

edit: my there is an error within this code within the sorting now i've it transformed( ought to be < rather than >)

This type of factor is performed most effectively utilizing an R-Tree. The JSI library supplies a Java implementation that I have tried personally effectively by having an index of 80.000 locations, processing 1000's of searches per second. However, it might not operate on Android.

i've not attempted running your code, however it appears enjoy it works, it is simply it's not capable. as if you don't really have to sort, you'll need the extract the minimum.

you are able to restrict your query to simply the square that's of size (2*MAX_SEARCH_DISTANCE)^2 (together with your point in the centre. By doing this you're localizing your query which will return you less leads to compute distance for. Obviously this can not help if all of your locations have been in the localized square (maybe unlikely?).

Also, I guess you could utilize hamiltonian distance rather than euclidean. euclidean distance = sqrt((lat0 - lat1)^2 + (lon0 - lon1)^2) hamitonian distance = (lat0 - lat1) + (lon0 - lon1)

I had been searching for something much the same a while ago:

http://stackoverflow.com/questions/2034348/android-sqlite-sort-on-calculated-column-co-ordinates-distance

I had been utilizing a MySQL research on my small server, MySQL enables you to produce a virtual column, works the calculation and sorts by distance, and you can set the max results came back or even the max distance - it is effective:

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 needed to do exactly the same operation during my application - pull all of the points to be able to distance in the customers location permitting me to exhibit the nearest ones. I wound up choosing the an answer like the main one recommended around the link above but understand its most likely not the perfect solution but works with the objective I needed.