Search code examples
javascriptfirebasefirebase-realtime-databasegeogeofire

Get the closest point to multiple points with GeoFire


I want to find the closest point to other multiple points (a point is represented as a 2-element array of [Latitude, Longitude]) from Firebase Realtime Database.

Imagine a food ordering app which includes multiple restaurants and multiple delivery persons of course, I want to find the delivery person who can deliver the food from multiple restaurants in multiple locations to the user.

Is this possible with GeoFire & Realtime Database or Firestore & GeoFirestore?


Solution

  • GeoFire (and other geoquerying systems based on geohashes) can only return items within a certain geographic range. There is no option to return only the closest item, so you will have to do that in your client-side code yourself.

    To implement this type of use-case, you'll typically:

    1. Start with (what you consider to be) a reasonable range.
    2. If you get any results, order them on distance and pick the closest one.
    3. If you get no results, expand the range and try again.