9

I am constructing an app (in XCode) which, in a general sense, displays information to users. The information is stored as individual objects in a database (happens to be a Parse-server hosted by heroku). The user can elect to "see" information that has been created within a set distance from their current location. (The information, when saved to the DB, is saved along with its lat and long based on the location of the user when they initiated the save). I know I can filter the pieces of information by comparing their lat and long to the viewing user's current lat and long and only display those which are close in enough. Roughly/generally:

var currentUserLat = latitude //latitude of user's current location
var infoSet = [Objects] //set of all pulled info from DB
for info in infoSet{
    if info.lat-currentUserLat < 3{//arbitrary value
       //display the info
    }else{
       //don't display
    }
}

This is set up decently enough, and it works fine. The reason it works fine, though, is because of the small number of entries in the DB at this current time (the app is in development). Under practical usage (ie many users) the DB may be full of information objects (lets say, a thousand). In my opinion, to individually pull and compare the latitude of the information and compare it to the current user's latitude for each and every DB entry would take too long. I know there must be a way to do it in a timely manner (think tinder... they only display profiles of people who are in the near vicinity and it doesn't take that long for them to do so despite millions of profiles) but I do not know what is most efficient. I thought of creating separate sections for different geographical regions in the DB and then only searching those particular section of the DB depending on where the user's current location is, but this seems unsophisticated and would still lead to large amounts of info being pulled. What is the best way to do this?

Runeaway3
  • 1,439
  • 1
  • 17
  • 43
  • What suggestions did you get googling? https://stackoverflow.com/q/574691/3404097 https://gis.stackexchange.com/questions/115766/fastest-strategy-for-proximity-searches-in-sql-server-2012 http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates – philipxy Jun 19 '17 at 03:32
  • @philipxy the DBMS is just the parse-server hosted by heroku. It employs mongoDB I believe, but I only really interact with the parse-server dashboard which heroku hosts – Runeaway3 Jul 05 '17 at 22:07
  • 1. Please edit clarifications into your question, not comments. 2. The general principle is you should try to query in the database/server. So "just" is not apropos. That is what the database is for. 3. If the current answers are inadequate, comment why, and also edit your question to ask for the kind of answers you want. Your question is not very specific; there's really no way to determine how a post could be said to answer it. – philipxy Jul 06 '17 at 03:59

2 Answers2

1

Is there a reason you need to do that sort of checking on the client side? I would suggest sending your coordinates to your server and then having the server query your database with those coordinates and figure out which items to pull based on the given coordinates respectively. Then you can have the server return back to the client side whichever items were "close" to that user

EDIT: reworded

TNguyen
  • 1,041
  • 9
  • 24
  • how would I initiate that comparison in the server as opposed to the client? – Runeaway3 Jun 14 '17 at 04:39
  • well, in your database i'm assuming you'll be storing users' location (through coordinates or what not). You can then create a route on your server that takes in a users location (and all other necessary things like authentication, etc). Then the server would query the database for other users that are located near those coordinates and then respond to the request with whatever it got from the database. Obviously i've left out a lot of the smaller details but the idea is there. – TNguyen Jun 14 '17 at 13:39