what I'm trying to do: the user selects the beginning and destination on the map, and then from my coordinates I want to show the nearest point location from the list of locations on the map. I have a simple Sqlite database containing the longitude, latitude, and name of possible locations.
I did some research, and here is what I found:
http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL
but it is intended to be used with mySql and some kind of spatial search extension. is there any way 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(); }
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)); }
where PointWithDistance is an object that contains: lat, long, distance, name
however, this solution does not provide the correct information about the return ... and I understand that it does not scale at all and is very slow. I need a solution that will execute quickly with a database with a maximum of 1000 rows.
edit: I had an error in this code in the sort, now I changed it (should be <instead of>)
java android database location
DArkO
source share