0
votes

I'm trying to get my head around GeoFire and I'm slightly confused into would GeoFire be suitable for what I'm trying to achieve.

I'm currently building a website (Angular 2, Typescript), this website allows users to sign up > create a profile and enter their location. This data is then stored within their profile as shown here:

enter image description here

The url to access this data is as follows:

https://my-website-f84a1.firebaseio.com/users<users auto generated key>/address

On the search page of this website, they can filter other users by given properties.

I then would like to sort the search results by closest first, which would be the following:

  1. Grab the location of the current user who is performing the search
  2. Then workout the distance of each user inside the search results against the location of the user who is performing the search then order by closest.

Seem's pretty straight forward right, well I'm confused and have some question.

  1. Would GeoFire be suitable for this?
  2. Would I need to load all user locations on page load and store within the geoFire ?

I'm not to sure if my current database structure for storing the user address information is suitable for GeoFire but any help would be appreciated.

1

1 Answers

0
votes

It's not clear whether you want to limit the distance for the results that are returned. GeoFire supports geoqueries by radius, but that requires you to specify the radius you want. If you want to return ALL matching resutls, then simply order them by distance from the user, then you are probably better off not using GeoFire.

Here's what I suggest;

  • Query the data, using the filtering properties you referred to.
  • Add each item to an array. Add a value in the array for the distance from the user's location to the filtered item's location
  • Sort the array by distance, and then use the array to display the data on screen

One comment about your Firebase data; it looks like you have your key ("id") in the actual record. The recommended way is to structure data so that the data is placed below the key in the JSON hierarchy. This allows you to optimize retrievals since the keys are indexed and unique. Here's an example of a database I use in one of my apps.

+ Cities
    + s0000001
        + nameEn: "Athabasca"
        + nameFr: "Athabasca"
        + provinceCode: "AB"
   + s0000002
        + nameEn: "Clearwater"
        + nameFr: "Clearwater"
        + provinceCode: "BC"