我正在将Android Studio 3.3.2与sdk版本28一起使用。 而且我需要帮助来实时保存位置平均纬度和用户的位置。
此代码是他们更新的Firebase的新实现。 由于数据库已更改其引用。使用身份验证时,会将用户ID保存到数据库中,但是使用Geofire检索数据时,则不会将其保存到Firebase数据库中
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);
}
}
我希望在我获得用户的纬度和经度时创建一个DriversAvailable列,该列将在用户每次移动时进行更新。