0

I am using Android Studio 3.3.2 with the sdk version 28. And I need help to save the location mean Latitude and the location of the user in real time.

This code is the new implementation of firebase they've updated. As database have change their references. When using authentification is save the user ID into the database but when using Geofire to retrieve the data is doesn't save it into the firebase database

public class DriverMapActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {

    private GoogleMap mMap;
    GoogleApiClient mGoogleApiClient;
    Location mLastLocation;
    LocationRequest mLocationRequest;
    private DatabaseReference mDatabase;
    private Button mLogout;

    @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));
        String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
        mDatabase = FirebaseDatabase.getInstance().getReference("DriversAvailable");
        GeoFire geoFire = new GeoFire(mDatabase);
        geoFire.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()));
     }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

    @Override
    protected void onStop() {
        super.onStop();

        String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
        mDatabase = FirebaseDatabase.getInstance().getReference("DriversAvailable");

        GeoFire geoFire = new GeoFire(mDatabase);
        geoFire.removeLocation(userId);
    }    
}

My expectation is to create a column DriversAvailable in when I'll get latitude and longitude of the user which will be updated everytime the user is moving.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
MarB
  • 1
  • 1
  • `super.onStop()` is being called too soon; `onPause()` and `onResume()` are not being considered. instancing `GeoFire` and `mDatabase` over and over is also questionable. is there anything in the logs, when calling `geoFire.setLocation()` ?? besides, `Firebase` does not have "columns". see: [MCVE](https://stackoverflow.com/help/mcve). – Martin Zeitler Apr 08 '19 at 11:32
  • If you put a breakpoint inside `onLocationChanged` and run it in the debugger, do you reach that method? If so, when you step through it, is `latLng` the value you'd expect? If so, is there any relevant message written to logcat when you step over `geoFire.setLocation()`? – Frank van Puffelen Apr 08 '19 at 14:27
  • Nothing is on the logs when calling geoFire.setLocation @MartinZeitler – MarB Apr 08 '19 at 19:19
  • @MarB I've once left this [answer](https://stackoverflow.com/a/52289410/549372), which should provide a good starting point; despite it only queries and does not update locations. at least it shows how to instance the handles only once. – Martin Zeitler Apr 08 '19 at 20:42

0 Answers0