Search code examples
androidfirebasefirebase-realtime-databasegeofire

GeoFire data has invalid format exception


I am setting up GeoFire for my Android app. At the start of retrieving data from the Firebase database, I am getting the following exception.

There was an error getting the GeoFire location: DatabaseError: User code called from the Firebase Database runloop threw an exception: W/System.err: java.lang.Throwable: GeoFire data has invalid format: {lon=74.529964, lat=31.4479225}

Apparently, it is retrieving data from the database but still throwing an exception :(

I have used the following code to retrieve data:

geoFire.getLocation("test1", new LocationCallback() {
        @Override
        public void onLocationResult(String key, GeoLocation location) {
            if (location != null) {
                System.out.println(String.format("The location for key %s is [%f,%f]", key, location.latitude, location.longitude));
            } else {
                System.out.println(String.format("There is no location for key %s in GeoFire", key));
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            System.err.println("There was an error getting the GeoFire location: " + databaseError);
        }
    });

As I'm new to firebase, is there anything obvious I am missing?This is how my firebase database looks like


Solution

  • GeoFire does not seem to use the keys lat and lon, or Latitude and Longitude as you have in your example, but some custom encoding of the location in a property called "g". The correct way to save locations for use by GeoFire seems to be the setLocation method provided by GeoFire.

    See How to save GeoFire coordinates along with other items in Firebase database? for a discussion of how the structure in your database should look like.