Get data from firebase when within distance

别来无恙 提交于 2020-01-05 16:41:51

问题


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



来源:https://stackoverflow.com/questions/19578303/get-data-from-firebase-when-within-distance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!