How to save location information in Firebase

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

In their blog post, GeoFire goes Mobile , they show what the data will look like - but how do I get this location filled in field?

enter image description here

I can save other types of strings in Firebase. I just use the code below. Question What data type should the location field have?

  Firebase ref = new Firebase("https://myfirebaselink.firebaseio.com/"); //User alan = new User("Alan Turing", 1912); alanRef.setValue(obj); 

I tried location as List<String> , but it didn’t work - the location field looked like this:

enter image description here

Edit: Further research found this post on the Google blog , but they are also saved as latitude1 and longitude keys . This probably was written before . This probably was written before GeoFire` was introduced.

+8
java android firebase geofire
source share
2 answers

The GeoFire project for Java has an excellent README that covers (among others) location data settings :

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

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

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

 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 remove it from the database, simply pass the location key removeLocation:

 geoFire.removeLocation("firebase-hq"); 
+6
source share

It looks here that the type of the object is GeoLocation, as in line 83.

+2
source share

All Articles