1

Formulas to Calculate Geo Proximity

(I am new in android development so please suggest me the best answer don't vote for downcast, and sorry for my bad english)

I am Using Haver Sine formula to calculate distance bt 2 points.

In above case Alix Axel uses mysql and php, but in my case i am implmenting this on my android app, actually in my app i have a LocationService (background) in which my app will continuesly fatch lon/lat using both GPS and Network provider using....

locationManager.requestLocationUpdates(provider, THIRTY_SEC, HUNDRED_METER, Locationlistener);

once a best is found than on the basic of that location i am performing sqlite indexng query, my sqlite query is (this method is synchronized in SQLiteHelper class)

SELECT * FROM (
    SELECT * FROM Places WHERE
        (Lat >= 1.2393 AND Lat <= 1.5532) AND (Lon >= -1.8184 AND Lon <= 0.4221)
) WHERE
    acos(sin(1.3963) * sin(Lat) + cos(1.3963) * cos(Lat) * cos(Lon - (-0.6981))) <= 0.1570;

as above this query results some rows which i am storing in local variable

List<Places> places = aboveQuery(location);

resultant places may be 1,100 or more, after that i am generating a simple notifications for each places in list. so notification still may be 1, 100 or more. Below is my code where i am performing this task.

public class LocationServices  extends Service {    

// rest of the code here to fatch location using GPS, Network 

private class LocationServiceProcess implements Runnable {

            @Override
            public void run() {
                try {
                List<Places> Places = query a databse for location matching

                // generate notification here

                } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
    }
}

i think now you guys can understand my problem, now my questions are

as Alix Axel said in his post that The Haversine Formula is way faster than the Vincenty Formula, I was able to run 1 million calculations in about 6 seconds which is pretty much acceptable for my needs

  1. Is this scnario is best to fetch places on the behalf of current location from database (supose in my database table contains thousends of places/rows). i.e this calculation and quering a data in service thread is best or not. (should i perform on another than Service main thread) if not than tell me what to do??

  2. After that once i have a list of places than, is it best idea to generate notifications in the same thread or brodcast all places and than in BroadCastReceiver generate notification for each places. Keep in mind just simple notifications, which contains only Name of places, Time to occur, and small description

Community
  • 1
  • 1

1 Answers1

0

An old question, but a common problem.

You could look at Mapsforge solution and how they manage POI searching. Have a closer look at POI search management, DbConstants and AbstractPoiPersistenceManager classes and specially study findNearPosition() method. Mapsforge team make a high quality job and logically performances fulfill my requirements (all categories can be selected and POI number is huge), perhaps it will fulfill yours.

Than you can use findNearPosition() in a class that extends Android AsyncTask and perform this method call in its doInBackground() method. onPostExecute(), you can collect results and update your UI. If position change, you can cancel the AsyncTask and launch another one.

chrisail
  • 21
  • 4