I'll try to walk you through your best option here. First off, I don't think storing the distance in Firebase makes a whole lot of sense. If you store the coordinates in Firebase, you can easily grab them and then calculate the distance client-side. If the distance is constantly changing (if the user changes position), then you will constantly be updating the distance in Firebase, which seems wasteful since you can just do that client-side.
That being said, let me get to your actual question. Your data structure would look like this:
var locationId = snapshot.key();
var location = snapshot.val();
geoFire.set("<location_id>", [location.latitude, location.longitude]).then(function() {
console.log(locationId + " has been added to GeoFire");
}).catch(function(error) {
console.log("Error adding " + locationId + " to GeoFire: " + error);
});
});
Note that since I'm assuming these locations won't move, you only need to add the locations to GeoFire one time. It makes sense to let some sort of server process handle this for you.
Now, when a new user uses your app, you can use whatever method to get their current location. Once you have that, create a new GeoQuery centered at their location:
// getUserLocation() is your own code somewhere
var userLocation = getUserLocation();
var geoQuery = geoFire.query({
center: [userLocation.latitude, userLocation.longitude],
radius: 5
});
We want to get notified whenever a location enters our query, so let's add a listener for that:
var onKeyEnteredRegistration = geoQuery.on("key_entered", function(key, location, distance) {
console.log(key + " entered query at " + location + " (" + distance + " km from center)");
});
Notice how GeoFire sends us the location (latitude / longitude pair) and the distance from the query center (aka the user) to the location. We don't need to store any of this information in Firebase like you suggested because GeoFire handles it for us.
If our user moves, we can update the query's center:
// users's location is now store in newLocation
geoQuery.updateCriteria({
center: [newLocation.latitude, newLocation.longitude]
});
You could also change the query radius if you wanted.
That should help you understand how this works on a step-by-step basis. Please look into
our many GeoFire examples for full code samples and more complex use cases.
Good luck!