firebase / geofire-js

GeoFire for JavaScript - Realtime location queries with Firebase
MIT License
1.45k stars 347 forks source link

Stuck On How To Handle Results #90

Closed alteredorange closed 8 years ago

alteredorange commented 8 years ago

Awesome library, looks great! I'm trying to use it instead of what I'm currently doing (run through a firebase array of clubs close to a user, calculate distances with the Haversine formula, and put clubs within a certian distance into an array, then ng-repeat through that array to show to the user).

So with GeoFire I got the query working, and I can get the unique IDs of each club within the radius logged to the console. But how can I ng-repeat or show that to the user?

Right now my geofire and club data looks like:

 "geoFire": {
       "uniqueClubID": {
               "g": "data",
               "i": "data"
                  }
}
"clubs": {
     "uniqueClubID": {
              "name": "club name here",
               "description": "This club does stuff"
              }
}

Any help would be greatly appreciated, thanks!

Oh, and I'm not trying to show a map, I'm just trying to show a list of the club names and descriptions to the user.

alteredorange commented 8 years ago

I made it work by pushing it into an array again, but is that the best way?

var array = [];
$scope.publicArray = array;
$scope.geoFunc = function() {
    geoQuery = geoFire.query({
    center: [userData.Latitude, userData.Longitude],
    radius: 25
  });
      geoQuery.on("key_entered", function(key, location, distance) {
    console.log(key + " is located at [" + location + "] which is within the query (" +    distance.toFixed(2) + " km from center)");
    var newRef = new Firebase(furl + "/clubs/" + key);
    $scope.thing = $firebaseObject(newRef);
    array.push($scope.thing);
  });
};
jwngr commented 8 years ago

Yup, that is the suggested way to handle this. Push onto your array as new data gets added. As noted in other issues raised previously in this repo, this may be getting easier if we decide to allow data to be stored alongside the GeoFire index.

alteredorange commented 8 years ago

Good to know, thanks!

RedFour commented 8 years ago

@jwngr I'm having trouble using the results of geoQuery.on in my Angular 2 project.

    var keys = [];

    var onKeyEnteredRegistration = this.geoQuery.on("key_entered", function (key, location, distance) {
      console.log(key + " entered query at " + location + " (" + distance + " km from center)");
      keys.push(key);
    });

    console.log(keys);
    for (let k of keys) {
      console.log(k);
    }
[]0: "-KPzA4RWQv5_bcNOpP9S"1: "-KPz8vrcPjsVrULOvgiN"2: "-KPzAVAHEJ2omCsBBLgX"3: "-KPz9X5d11iGEM5n3Uy-"length: 4__proto__: Array[0]
prof-search.component.ts:29 -KPzA4RWQv5_bcNOpP9S entered query at 34.0247213,-118.4110094 (0.7816577676665337 km from center)
prof-search.component.ts:29 -KPz8vrcPjsVrULOvgiN entered query at 34.0188461,-118.4076125 (0.4544501661981215 km from center)
prof-search.component.ts:29 -KPzAVAHEJ2omCsBBLgX entered query at 34.0075845,-118.4259107 (2.5550449190008173 km from center)
prof-search.component.ts:29 -KPz9X5d11iGEM5n3Uy- entered query at 34.007981,-118.4131563 (1.7175106506069682 km from center)

Console.log works when it is applied to the keys array; however, using keys in a for-loop shows it is undefined.

alteredorange's solution seems to be for Angular 1. Could you show me how I should be using it in Angular 2?

RedFour commented 8 years ago

Nvm. Just returned the results as a promise and got things working.

frenchynyc commented 7 years ago

Hello, I tried your solution but I can't find a way to sort it out...

If you could look at my problem here : https://github.com/firebase/geofire-js/issues/141

Thank you for your help !