How to get location updates using FusedLocationProviderClient?

Viewed 49

I'm an Android noob and also this is my first post on stackoverflow. I've been trying for some time to find a solution to my problem but couldn't find anything so that's why I'm asking here.

I'm trying to make an app that has a courier activity and a client activity. In the courier activity i'm trying to get the current location of the user periodically (preferably every few seconds). I've seen several examples online with LocationRequest() but when i try it in my code it says that it's deprecated now.

Here is my code so far:

public class CurierMapActivity extends FragmentActivity implements OnMapReadyCallback {


private GoogleMap mMap;
private ActivityCurierMapBinding binding;

private Button btn_logout;

@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = ActivityCurierMapBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    btn_logout = findViewById(R.id.btn_logout_curier);
    btn_logout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FirebaseAuth.getInstance().signOut();
            Intent intent = new Intent(CurierMapActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    });
}

@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onMapReady(GoogleMap googleMap) {
    checkLocationPermission();

    FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
            if(location != null) {
                LatLng my_position = new LatLng(location.getLatitude(), location.getLongitude());
                mMap.addMarker(new MarkerOptions().position(my_position).title("My position"));
                mMap.moveCamera(CameraUpdateFactory.newLatLng(my_position));
            } else {
                Log.d("locatie", "Locatia nu a fost transmisa");
            }
        }
    });
}


@RequiresApi(api = Build.VERSION_CODES.M)
public void checkLocationPermission() {
    if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_FINE_LOCATION)) {

        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    1234);
        }
    }
}
}

Right now I'm only getting the current location once and I can't seem to figure out how to get it every few seconds and also transfer it to the other activity.

Please help

0 Answers
Related