0

I am creating an app that has a features that needs to find supermarkets that are in a 5km range from the user.

I thought that using longitude and latitude would be a good idea (I should store those two values as atributes on a supermarket table and I'd be ready to go).

Once I created my table and inserted it's values, I went to look for a solution on how could I get those supermarkets from my database. What I had in mind would be a select with a not so complex WHERE clause that would solve all my problems. Instead, I found a bunch of different ways of doing it:

1) Create an square around the user location, find the supermarkets that are inside of it and then find the ones that are actually in that range

2) Create extra coluns on your table

3) Use the cosine law

4) Find out how to convert latitude/longitude to kilometers and use Pythagorean Theorem (for ranges that are not so wide)

The three problems are that any solution is made on the database (with a where clause), I have no idea which one has more performance or is better in any aspect and it's not like I'm getting how to use those exemples. So... What should I use remembering that I have and SQLite database and android?

Thanks :)

ADDING MORE INFORMATION:

The first problem was to know which one I should use. The problem I had with this one was the this:

My user location is:

longitude = -22.82753105; latitude = -47.03398022;

My where statement

WHERE latitude < -47.078946300295925 AND
latitude > -46.989014139704054 AND
longitude > -22.76155626735494 AND
longitude < -22.89350583264506

I have a market on my database which location is the same the user and I do get a list with this very element once I select the ones that are in the square, but when I iterate on my list, it removes my element from the list bacause the if return FALSE.

if (getDistanceBetweenTwoPoints(mercado, userLocation) <= RANGE)

On the getDistanceBetweenTwoPoints, if I debug it, I get:

dLat = -0.4224822382331485
dLon = 0.4224822382331485
lat1 = -0.39841557692373836
lat2 = -0.8208978151568869
a = 0.07157979929009457
c = 0.5416864422451098
d = 3451084.323543594

And since the if compares "d" with range, it's like this:

if(3451084.323543594 <= 5000) { //my range is 5km
// keep element
else
// remove it from list

Does anyone can tell me what is the problem??

Community
  • 1
  • 1
Leonardo Sibela
  • 1,159
  • 1
  • 13
  • 34
  • See this link http://stackoverflow.com/questions/3695224/sqlite-getting-nearest-locations-with-latitude-and-longitude – KishuDroid Oct 08 '15 at 04:09
  • @KishuDroid That's one of the question he already linked to! – CL. Oct 08 '15 at 09:26
  • 1
    What specific problem do you have with that code? – CL. Oct 08 '15 at 09:28
  • @cl. I add some extra information on my question. Thank you very much :) – Leonardo Sibela Oct 10 '15 at 16:12
  • 1
    The variable values are wrong. Show the actual values of `mercado` and `userLocation`, and the actual code of your `getDistanceBetweenTwoPoints`. – CL. Oct 10 '15 at 18:05
  • @CL. thank you very, very much for both your patience and help. I keep debugin it and found that I made a mistake. The method that turns my Cursor to a Mercado (Market object) was puting on my Mercado.longitude the latitude value from my database and on my Mercado.latitude the longitude value =\ That's why my select was ok but when I was iterating on my list, I was having a problem. If it's not ask too much, could I ask you one more thing, please? – Leonardo Sibela Oct 11 '15 at 03:50
  • To ask a question, use the "Ask Question" button. – CL. Oct 11 '15 at 07:09

0 Answers0