我正在使用Geofire来获取当前位置并将其保存在Firebase中。但是id不知道如何从Firebase检索它。我已经尝试过StackOverflow上可用的各种代码,但是我无法检索它并在其上放置标记。请帮助我提供确切的代码。
public class LocationMapsActivity extends FragmentActivity implements OnMapReadyCallback,GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
private GoogleMap mMap;
private SupportMapFragment mapFragment;
private DatabaseReference ref;
private FirebaseUser user;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
LocationRequest mLocationRequest;
Boolean isLoggingOut=false;
Button logout1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_maps);
mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(LocationMapsActivity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},LOCATION_REQUEST_CODE);
}
else {
mapFragment.getMapAsync(this);
}
/* logout1=(Button)findViewById(R.id.btnLogout);
logout1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isLoggingOut=true;
disconnectServiceProvider();
FirebaseAuth.getInstance().signOut();
Intent intent=new Intent(ServiceProviderMapActivity.this,MainActivity.class);
startActivity(intent);
finish();
return;
}
});*/
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
FirebaseAuth firebaseAuth=FirebaseAuth.getInstance();
String userid=FirebaseAuth.getInstance().getUid();
user=firebaseAuth.getCurrentUser();
final DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference("ServiceProvidersLocations").child(user.getUid());
final GeoFire geoFire = new GeoFire(dbRef);
geoFire.getLocation("Locations",new LocationCallback() {
@Override
public void onLocationResult(String key, GeoLocation location) {
LatLng locTION= new LatLng(location.latitude,location.longitude);
mMap.addMarker(new MarkerOptions().position(locTION).title("Name")).setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
/* // Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));*/
}
protected synchronized void buildGoogleApiClient(){
mGoogleApiClient=new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onLocationChanged(Location location) {
mLastLocation=location;
LatLng latLng=new LatLng(location.getLatitude(),location.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Marker is on your current location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(18));
String userId=FirebaseAuth.getInstance().getCurrentUser().getUid();
ref=FirebaseDatabase.getInstance().getReference("serviceConsumers");
GeoFire geoFire=new GeoFire(ref);
geoFire.setLocation(userId,new GeoLocation(mLastLocation.getLatitude(),mLastLocation.getLongitude()), new GeoFire.CompletionListener() {
@Override
public void onComplete(String key, DatabaseError error)
{
}
});
}
@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, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(LocationMapsActivity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},LOCATION_REQUEST_CODE);
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
}
final int LOCATION_REQUEST_CODE=1;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode){
case LOCATION_REQUEST_CODE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mapFragment.getMapAsync(this);
}
else {
Toast.makeText(getApplicationContext(),"Please Provide Permission to use application ",Toast.LENGTH_LONG).show();
}
break;
}
}
}
private void disconnectServiceProvider()
{
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
String userId=FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference ref=FirebaseDatabase.getInstance().getReference("serviceProvidersAvailable");
GeoFire geoFire=new GeoFire(ref);
geoFire.removeLocation(userId);
}
@Override
protected void onStop() {
super.onStop();
if(!isLoggingOut)
{
disconnectServiceProvider();
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
非常感谢您。