0

I was getting a current device location using FusedLocationProviderClient`

com.google.android.gms.tasks.Task<Location> location = mFusedLocationProviderClient.getLastLocation();
                location.addOnCompleteListener(new OnCompleteListener<Location>() {
                    @Override
                    public void onComplete(@NonNull com.google.android.gms.tasks.Task<Location> task) {
                        if(task.isSuccessful()){
                            Location currentLocation = task.getResult();

                            Toast.makeText(getApplicationContext(),"Detecting current Location",Toast.LENGTH_LONG).show();

                            moveCamera(new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude()),ZOOM,"My Location");


                            mUserLocalStore = new UserLocalStore(getApplicationContext());
                            db = FirebaseDatabase.getInstance();
                            mRef = db.getReference("Location");
                            mRef.setValue(String.valueOf(mUserLocalStore.getDetails().getEmail()+" "+currentLocation.getLatitude()+" ,"+String.valueOf(currentLocation.getLongitude())));

and save location in firebase, How to update location to firebase when change location or given in to the time period`

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121

1 Answers1

2

Check this little example, it should solve your problem. Please check onConnected() method, there is the logic you need, I also let you all the code that make it work all together if you need:

 package current_location_to_firebase.mytrendin.com.currentlocation;


    import android.location.Location;
    import android.os.Bundle;

    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.api.GoogleApiClient;
    import com.google.android.gms.location.LocationServices;
    import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
    import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
    import com.google.firebase.FirebaseApp;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;


    public class MainActivity extends AppCompatActivity implements
            ConnectionCallbacks, OnConnectionFailedListener {

        private static final String TAG = "CurrentLocationApp";
        private GoogleApiClient mGoogleApiClient;
        private Location mLastLocation;
        private TextView mLatitudeText;
        private TextView mLongitudeText;
        private FirebaseDatabase mFirebaseDatabase;
        private DatabaseReference mLocationDatabaseReference;
        Button saveLocationToFirebase;
        String value_lat = null;
        String value_lng=null;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            FirebaseApp.initializeApp(this);
            mFirebaseDatabase = FirebaseDatabase.getInstance();
            mLocationDatabaseReference= mFirebaseDatabase.getReference().child("my current location");
            mLatitudeText = (TextView) findViewById((R.id.latitude_text));
            mLongitudeText = (TextView) findViewById((R.id.longitude_text));
            saveLocationToFirebase=(Button)findViewById(R.id.save_location);
            buildGoogleApiClient();
        }

        protected synchronized void buildGoogleApiClient() {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
        }

        @Override
        protected void onStart() {
            super.onStart();
            mGoogleApiClient.connect();
        }

        @Override
        protected void onStop() {
            super.onStop();
            if (mGoogleApiClient.isConnected()) {
                mGoogleApiClient.disconnect();
            }
        }
        @Override
        public void onConnected(Bundle connectionHint) {
            mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

                if (mLastLocation != null) {

                            value_lat= String.valueOf(mLastLocation.getLatitude());
                            value_lng =String.valueOf(mLastLocation.getLongitude());
                    mLatitudeText.setText(value_lat);
                    mLongitudeText.setText(value_lng);

                    saveLocationToFirebase.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mLocationDatabaseReference.push().setValue("Latitude : "+value_lat +"  &amp; Longitude : "+value_lng);
                            Toast.makeText(MainActivity.this ,"Location saved to the Firebasedatabase",Toast.LENGTH_LONG).show();
                        }
                    });
                }
        }
        @Override
        public void onConnectionFailed(ConnectionResult result) {
            Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
        }
         @Override
        public void onConnectionSuspended(int cause) {
            Log.i(TAG, "Connection suspended");
            mGoogleApiClient.connect();
        }

    }

Remember in your database rules to set it to true if they are turned false for just auth users to post into it

enter image description here

saveLocationToFirebase is a button that should be initialized in your XML, so when we click it you update the values to the database, if you want them to do it by itself attach a listener to the latitude and longitude, so, when they change it will prompt the upload to the database.

halfer
  • 19,824
  • 17
  • 99
  • 186
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77