Handling geocoding in Firebase?
Asked Answered
D

2

7

Has anyone tried storing and/or searching on geocodes (e.g. lat/long) in Firebase? This is functionality that is built into MongoDB and as I'm considering using Firebase for our backend instead, I need to know how I'd handle this scenario in Firebase.

Thanks!

Dave answered 18/1, 2014 at 20:28 Comment(0)
M
9

The folks at Firebase recently open-sourced a library that allows you to store and query location data in Firebase. In short, this is very easily accomplished because all keys in Firebase are strings, and Firebase has support for startAt() and endAt() queries that allow you to do the appropriate windowing for geohashes and bounding boxes.

For implementation details and usage, check out the live demos, source code, and their blog post on GeoFire.

Marcelinomarcell answered 18/1, 2014 at 21:35 Comment(0)
B
0

Hey I just finished building a real time google map using firebase and GeoFire. GeoFire is really cool and easy to use. It allows you to query using lon lat and radius. It returns a key that you can use to query your firebase db. You set the key, while you create the geoFire object, to be whatever you want. It is usually a ref that you can use to get the object that is associated with that distance.

Here is a link to geoFire: https://github.com/firebase/geofire-js

Here is an example use case:

You have a lon lat, that you got using navigator:

var lon = '123.1232';
var lat = '-123.756';
var user = {
    name: 'test user',
    longitude: lon,
    latitude: lat
}
usersRef.push(user).then(function(response) {
    var key = response.key;
    var coords = [lon, lat];
    geoFire.set(key, coords, function(success){
         console.log('User and geofire object has been created');
    });
})

Now you can query for the user using:

// Set your current lon lat and radius
var geoQuery = geoFire.query({
    center: [latitude, longitude],
    radius: radiusKm
});
geoQuery.on('key_entered', function(key, location, distance) {
    // You can now get the user using the key
    var user = firebaseRefUrl + '/' + key;

    // Here you can create an array of users that you can bind to a   scope in the controller
});

If you are using google maps. I reccomend you use angular-google-maps. Its a really cool google maps directive that takes in an array of markers and circles. So when ever $scope.markers or $scope.circles change in the controller it will automatically be applied to the map without any messy code. They have very good documentation.

Here is a link: http://angular-ui.github.io/angular-google-maps/

Bequest answered 27/3, 2015 at 21:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.