java - How to Use SQL Where Conditional For Distance Between a Row's Lat & Long, & Current Lat & Long -
this question has answer here:
so i'm writing android app , want write sqlite query such selects rows contain event location info utilizing distance constraint in miles , current longitude , latitude , ordering calculated distance. once i've gotten further along sqlite database instead make query on database on azure may or may not open functionality, rather querying sqlite database that's inserted mock data. since i'm working sqlite atm i'm trying come android-sqlite solution.
the sqlite table schema such:
event_id | event_name | event_latitude | event_longitude
below pseudocode mixed real code idea of i'm trying create:
sqlite_table = "some_table"; double distanceconstraint = 50.00; //in miles //using decimal degrees (ddd) it's easiest calculate currentlat = 41.40338; currentlong = 2.17403; mcursor = mdb.query(true, sqlite_table, new string[] {event_id, event_name}, /* (where) current lat & long within distancecontraint of event_latitude & event_longitude*/, null, null, null, /* distancebetweenpointsresult */ + " desc", null);
how write query calculate distance (in miles) between current location , event's , filter , order accordingly?
background: using haversine formula
to calculate distance between 2 coordinates, seems common practice use haversine formula. here's specific post on mysql haversine-based query: https://stackoverflow.com/a/574736/3784227
the implementation suggests based on following google maps api article: https://developers.google.com/maps/articles/phpsqlsearch_v3#findnearsql
a proposed solution question
here's (untested) attempt @ sql query solve problem. use haversine formula mentioned above; version google maps api discusses.
"select event_id, event_name, event_latitude, event_longitude, (3959 * acos( cos( radians(event_latitude) ) * cos( radians(" + currentlat + ") ) * cos( radians(" + currentlon + ") - radians(event_longitude) ) + sin( radians(event_latitude) ) * sin(radians(" + currentlat + ")) ) ) distance " + sqlite_table + " having distance < " + distanceconstraint + " order distance limit 0, 20;"
if i'm not mistaken, should return event_id
, event_name
, event_latitude
, , event_longitude
20 locations within distanceconstraint
radius {currentlat
, currentlon
}, ordered distance
.
i'm not familiar how implement query android / sqlite, helps point in right direction.
Comments
Post a Comment