Creating query to return a CursorLoader sorted by distance from current location

94 Views Asked by At

Good day, I have been looking and looking for something similar but so far I haven't found anything related to my issue.

I have an sqlite db where a have a bunch of places stored by its geolocation such as this:

"CREATE TABLE " + WifiEntry.TABLE_NAME + " ( "
            + WifiEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + WifiEntry.COLUMN_NAME + " TEXT NOT NULL, "
            + WifiEntry.COLUMN_LONGITUDE + " REAL, "
            + WifiEntry.COLUMN_LATITUDE + " REAL, "
            + WifiEntry.COLUMN_OPINION + " REAL DEFAULT 0 );";

Then I have a content provider to take care of the CRUD ops, and loaders to access it. I also have a Class called GeoPoint to calculate ths distance between two points

public class GeoPoint {
private double lng, lat;
...

   public GeoPoint(double lng, double lat)
   {
      this.lng= lng;
      this.lat= lat;
   }

   public double distance(GeoPoint p)
   {
      returns the distance between this geopoint and p...
   }

As you can see I don't have a column in the db for the stored distance from currentLocation, and the places since this changes regularly. I can easily calculate the distance from where I am and the place, but how can I sort by distance to show in the ListView?

In onCreateLoader I have this, and as you can see, I sort by name fine, but, how to sort by current distance from the place, how to put that in the Projection?

String [] projection = new String[]
                    {
                            WifiEntry._ID,
                            WifiEntry.COLUMN_NAME,
                            WifiEntry.COLUMN_LATITUDE,
                            WifiEntry.COLUMN_LONGITUDE,
                            WifiEntry.COLUMN_OPINION
                    };
            SharedPreferences prefs =
                    PreferenceManager.getDefaultSharedPreferences(getActivity());
            int sortPref = Integer.parseInt(prefs.getString(
                    getString(R.string.settings_sort_criteria_key), "0"));
            switch (sortPref)
            {
                case 0:
                    return new CursorLoader(
                            getActivity(),          // Parent activity context
                            WifiEntry.CONTENT_URI,  // Provider content URI to query
                            projection,             // Columns to include in the resulting Cursor
                            null,                   // No selection clause
                            null,                   // No selection arguments
                            WifiEntry.COLUMN_WIFI_NAME + " ASC");
                case 1: 
                // this is the **bydistance CASE**
                String sel =
                        WifiEntry.COLUMN_LONGITUDE + " " +
                                currentPosition.getLongitud()

                    WifiEntry.COLUMN_LATITUDE + " " +
                            currentPosition.getLatitud()
                           ??? 
                    //COLUMN1 + " * " + COLUMN2 + " as data",

Thanks!!

0

There are 0 best solutions below