how to save Location Info in Firebase
Asked Answered
D

2

10

I am trying to save location (so latitide and longitude) as one of the keys/fields in Firebase. In their example SFVehicles, they do show how to query once the information is stored but my problem is how do i save in the first place.

In their blog post, GeoFire goes Mobile, they are showing how the data would look like - but how do I get that location field populated?

enter image description here

I am able to save other types of strings to the Firebase though. I just use the code below. Question is What data type should the location field be?

        Firebase ref = new Firebase("https://myfirebaselink.firebaseio.com/");

       //User 
       alan = new User("Alan Turing", 1912);


        alanRef.setValue(obj);

I tried location to be a List<String>, but that did not work -- the location field looked like below:

enter image description here

Edit: On more research, found this blog post by Google but they are also saving as keys latitude1 and longitude. This probably was written before GeoFire` was introduced.

Destruct answered 10/4, 2016 at 18:52 Comment(2)
You've included a picture of the JSON tree in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export button in your Firebase database. Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do.Kerouac
The googlegeodeveloper blog post does not use GeoFire. So it's irrelevant to you if you're looking to use GeoFire.Kerouac
K
7

The GeoFire for Java project has a great README, that covers (amongst other) setting location data:

In GeoFire you can set and query locations by string keys. To set a location for a key simply call the setLocation() method. The method is passed a key as a string and the location as a GeoLocation object containing the location's latitude and longitude:

geoFire.setLocation("firebase-hq", new GeoLocation(37.7853889, -122.4056973));

To check if a write was successfully saved on the server, you can add a GeoFire.CompletionListener to the setLocation() call:

geoFire.setLocation("firebase-hq", new GeoLocation(37.7853889, -122.4056973), new GeoFire.CompletionListener() {
    @Override
    public void onComplete(String key, FirebaseError error) {
        if (error != null) {
            System.err.println("There was an error saving the location to GeoFire: " + error);
        } else {
            System.out.println("Location saved on server successfully!");
        }
    }
});

To remove a location and delete it from the database simply pass the location's key to removeLocation:

geoFire.removeLocation("firebase-hq");
Kerouac answered 10/4, 2016 at 20:38 Comment(1)
Thanks, the readme is quite good and explains all operations clearly. Silly i missed it, thanks.Destruct
C
2

It looks from here That the type of the object is GeoLocation , like in line 83.

Cornelison answered 10/4, 2016 at 19:40 Comment(1)
the link is outdated :(Reviel

© 2022 - 2024 — McMap. All rights reserved.