1

I'm setting up a current location in my application.Where do i need to set the encoding?

    @Override
public void onLocationChanged(Location location) {
    mLastLocation = location;
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(11));

    //Geo Fire for current location
    String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference refAvailable = FirebaseDatabase.getInstance().getReference("driversAvailable");
    DatabaseReference refWorking = FirebaseDatabase.getInstance().getReference("driversWorking");

    GeoFire geoFireAvailable = new GeoFire(refAvailable);
    GeoFire geoFireWorking = new GeoFire(refWorking);

    switch (customerId){
        case "":

            geoFireWorking.removeLocation(userId);
            geoFireAvailable.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
                @Override
                public void onComplete(String key, DatabaseError error) {

                }
            });

            break;
            default:
                geoFireAvailable.removeLocation(userId);
                geoFireWorking.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
                    @Override
                    public void onComplete(String key, DatabaseError error) {

                    }
                });

                break;
    }


}

Following is error which I'm getting:

java.lang.NoSuchMethodError: com.google.firebase.database.DatabaseReference.setValue at com.firebase.geofire.GeoFire.removeLocation(GeoFire.java:215) at com.firebase.geofire.GeoFire.removeLocation(GeoFire.java:192) at com.example.webforest.quickaidlikeuber2nd.DriverMapActivity.onLocationChanged(DriverMapActivity.java:184)

Raifur-rahim
  • 543
  • 1
  • 3
  • 12
  • Can you try using `geoFire.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener(){ });` instead – Nero Jan 02 '19 at 09:36
  • It's worked!!!!! Thank you very much – Raifur-rahim Jan 02 '19 at 10:00
  • Glad I could help... I'll post this as an answer so it can be removed from unanswered questions list. – Nero Jan 02 '19 at 10:08

2 Answers2

1

Geofire has changed a bit, now to make a removeLocation work you have to add a listener like this:

 //after updating 
                    geoFireWorking.removeLocation("id of the node you want to remove", new GeoFire.CompletionListener() {
                        @Override
                        public void onComplete(String key, DatabaseError error) {

                        }
                    });
Raifur-rahim
  • 543
  • 1
  • 3
  • 12
0

This bug appears to manifest automatically when you are using setLocation() method without the use of GeoFire.CompletionListener(). If you implement the CompletionListener, this should resolve the issue.

Therefore, replace

geoFire.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()));

with

geoFire.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener(){ });

Edit 2:
You may need to adapt to a different approach if you are still encountering difficulties.

Replace the following code:

geoFireAvailable.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
    @Override
    public void onComplete(String key, DatabaseError error) {

    }
});

With the following code:

GeoHash geoHash = new GeoHash(new GeoLocation(location.getLatitude(),location.getLongitude()));
Map<String, Object> updates = new HashMap<>();
updates.put("g", geoHash.getGeoHashString());
updates.put("l", Arrays.asList(location.getLatitude(),location.getLongitude()));
geoFireAvailable.setValue(updates,geoHash.getGeoHashString());
Nero
  • 1,058
  • 1
  • 9
  • 25
  • i face again same problem while i use switch case in \n switch (customerId){ case "": geoFireWorking.removeLocation(userId); geoFireAvailable.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() { @Override public void onComplete(String key, DatabaseError error) { } }); – Raifur-rahim Jan 03 '19 at 09:41
  • @Raifur-rahim edit the question with the new code, problem and what you are trying to do. I'll review it – Nero Jan 03 '19 at 09:52
  • @Raifur-rahim check the answer again – Nero Jan 03 '19 at 11:55
  • Sir I'm new comer if u kindly describe specifically then it will be helpful – Raifur-rahim Jan 03 '19 at 12:54
  • @Raifur-rahim lol, don't refer to me as 'Sir'... I know it's respectful but I am not at that level yet. I've edited the answer, have another look – Nero Jan 03 '19 at 12:58
  • u r a respectable person for me .That's why i'm happy to mention u as a sir – Raifur-rahim Jan 03 '19 at 13:45
  • I check the answer again but now it says 'error: cannot find symbol method setValue(Map,String)' @Nero – Raifur-rahim Jan 10 '19 at 07:29