Get data from firebase when within distance

2019-08-07 09:26发布

问题:

I want to : GET data (=previous chat messages) from Firebase when entering 300m of a chatroom geolocation.

My Code :

Here is my basic Firebase chat function :

var myDataRef = new Firebase("https://digitaldatastrategi.firebaseio.com/");
  $('#messageInput').keypress(function (e) {
    if (e.keyCode == 13) {
      var name = $('#nameInput').val();
      var text = $('#messageInput').val();
      myDataRef.push({name: name, text: text});
      $('#messageInput').val('');
  }

});

myDataRef.on('child_added', function(snapshot) {
var message = snapshot.val();
displayChatMessage(message.name, message.text);

});

function displayChatMessage(name, text) {
    $('<div/>').text(text).prepend($('<em/>').text(name+': ')).appendTo($('#messagesDiv'));
    $('#messagesDiv')[0].scrollTop = $('#messagesDiv')[0].scrollHeight;
  };

Here is my "activate Chatroom when within 300m" function :

if (chatroom) {

jQuery("#chat").slideDown("slow");
myDataRef    = new Firebase(firebaseURL + chatroom[0]);

console.log("You are in:", chatroom);

}

else {

jQuery("#chat").slideUp("slow");
console.log("You are not in a chatroom");

} }

How can I do so not only does a chatroom active when I get within 300m but I ALSO get the data from firebase that is located at that chatroom. I can't manage to make the function(snapshot) to work properly. Only "undefined" appears in the chatroom window.

much appreciated. /a.

回答1:

Take a look at GeoFire, a library built on top of Firebase that lets you perform GeoQueries such as these: https://www.firebase.com/blog/2013-09-25-location-queries-geofire.html