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.