0

A list of users with their location (long & lat) and a radius (in kilometers) is given in my firebase database, effectively defining individual "action circles", e.g.

{
  "users": {
    "foo": {
      "lat": 11.2549387,
      "long": 17.3419559,
      "radius": 50
    },
    "bar": {
      "lat": 9.5123445,
      "long": 17.1221547,
      "radius": 80
    }
  }
}

My goal is now to query all users whose circle covers a specific location (again long & lat). Since the list of users can be very long it makes no sense to simply calculate the distance to the location for every single user each time.

I am aware of GeoFire for JavaScript, but this does not solve my problem completely because it is not suitable for calculations with a non-fixed radius.

The solution should be either client-side (JavaScript) or as a cloud function. Any ideas on that?

sonovice
  • 813
  • 2
  • 13
  • 27

1 Answers1

0

Well GeoFire is the solution for you because it will optimize the query for you.

I am aware of GeoFire for JavaScript, but this does not solve my problem completely because it is not suitable for calculations with a non-fixed radius.

It will work with the non-fixed radius you just need to update your query's radius and/or lat long with the new user and it will get the nearby users.

For Geofire you need an extra seprate node to store each user's geolocation object

Umar Hussain
  • 3,461
  • 1
  • 16
  • 38
  • Thanks for your answer. I'm afraid that doing this might lead to a pretty long response time for my end user (several hundred users in database). Or am I maybe overestimating the processing time? – sonovice Aug 27 '17 at 10:43
  • It will be optimized, geofire will index data on geohash so instead of calculating distance it will use geohash to find nearest items. Do check out the benefits of using geohash. – Umar Hussain Aug 27 '17 at 10:46
  • So processing time will not be very long – Umar Hussain Aug 27 '17 at 10:46
  • That would still mean to update the criteria for GeoFire for every single user in the database each time I am making such a request, resulting in hundreds of queries. Am I missing something or are we maybe talking past each other? – sonovice Aug 27 '17 at 10:52
  • You want to read a user node and then ask geofire i have this location and this radius show me other users in this radius right? – Umar Hussain Aug 27 '17 at 10:55
  • Almost. Imagine a map where every user is drawn into by means of a circle with their individual radius. I am now picking a point on that map and would like to get all users whose circle covers my point. – sonovice Aug 27 '17 at 11:00
  • Well this is different you have all the users on the map already and you want to see which one are covering a certain point on map. It will be an expensive operation you have to check that point with other user currently visible on map – Umar Hussain Aug 27 '17 at 11:07
  • To optimize you can limit the user added to the map at one point – Umar Hussain Aug 27 '17 at 11:09
  • Unfortunately this is not an option. Thanks anyways for your help! – sonovice Aug 27 '17 at 11:12