title: Find elements within 5km of a given longitude/latitude
body: <p>I am creating an app that has a features that needs to find supermarkets that are in a 5km range from the user.</p>
  
  <p>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).</p>
  
  <p>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:</p>
  
  <p>1) <a href="https://stackoverflow.com/questions/3695224/sqlite-getting-nearest-locations-with-latitude-and-longitude">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</a></p>
  
  <p>2) <a href="https://stackoverflow.com/questions/31544022/sqlite-select-items-based-on-distance-latitude-and-longitude">Create extra coluns on your table</a></p>
  
  <p>3) <a href="http://www.movable-type.co.uk/scripts/latlong-db.html" rel="nofollow noreferrer">Use the cosine law</a></p>
  
  <p>4) Find out how to convert latitude/longitude to kilometers and use Pythagorean Theorem (for ranges that are not so wide)</p>
  
  <p>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?</p>
  
  <p>Thanks :)</p>
  
  <p>ADDING MORE INFORMATION:</p>
  
  <p>The first problem was to know which one I should use.
  The problem I had with this one was the this:</p>
  
  <p>My user location is:</p>
  
  <p>longitude = -22.82753105;
  latitude = -47.03398022;</p>
  
  <p>My where statement</p>
  
  <p>WHERE latitude &lt; -47.078946300295925 AND<BR>
  latitude > -46.989014139704054 AND<BR>
  longitude > -22.76155626735494 AND<BR>
  longitude &lt; -22.89350583264506
      <BR><BR>
  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.</p>
  
  <p>if (getDistanceBetweenTwoPoints(mercado, userLocation) &lt;= RANGE)</p>
  
  <p>On the getDistanceBetweenTwoPoints, if I debug it, I get:</p>
  
  <p>dLat = -0.4224822382331485 <BR>
  dLon = 0.4224822382331485<BR>
  lat1 = -0.39841557692373836<BR>
  lat2 = -0.8208978151568869<BR>
  a = 0.07157979929009457<BR>
  c = 0.5416864422451098<BR>
  d = 3451084.323543594</p>
  
  <p>And since the if compares "d" with range, it's like this:</p>
  
  <p>if(3451084.323543594  &lt;= 5000) { //my range is 5km<BR>
   // keep element<BR>
  else<BR>
   // remove it from list<BR></p>
  
  <p>Does anyone can tell me what is the problem??</p>