From b90378264aad3a81b9a9e4bbb8ee41ba8141a04c Mon Sep 17 00:00:00 2001 From: Romuald DANSOU Date: Mon, 25 Jun 2018 01:05:39 +0100 Subject: [PATCH] Add an updateLocation function to update location without delete other data --- .../java/com/firebase/geofire/GeoFire.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/common/src/main/java/com/firebase/geofire/GeoFire.java b/common/src/main/java/com/firebase/geofire/GeoFire.java index 1d0f20f..19b8bf5 100644 --- a/common/src/main/java/com/firebase/geofire/GeoFire.java +++ b/common/src/main/java/com/firebase/geofire/GeoFire.java @@ -183,6 +183,35 @@ public void onComplete(DatabaseError databaseError, DatabaseReference databaseRe } } + /** + * Update the location for a given key. + * + * @param key The key to save the location for + * @param location The location of this key + * @param completionListener A listener that is called once the location was successfully saved on the server or an + * error occurred + */ + public void updateLocation(final String key, final GeoLocation location, final CompletionListener completionListener){ + if (key == null) { + throw new NullPointerException(); + } + DatabaseReference keyRef = this.getDatabaseRefForKey(key); + GeoHash geoHash = new GeoHash(location); + Map updates = new HashMap<>(); + updates.put("g", geoHash.getGeoHashString()); + updates.put("l", Arrays.asList(location.latitude, location.longitude)); + if (completionListener != null) { + keyRef.updateChildren(updates, new DatabaseReference.CompletionListener() { + @Override + public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) { + completionListener.onComplete(key, databaseError); + } + }); + } else { + keyRef.updateChildren(updates); + } + } + /** * Removes the location for a key from this GeoFire. *