I am trying to return the latitude and longitude of my current location using an emulator. I am getting an error that I need to allow ACCESS_FINE_LOCATION even though this is already in my manifest file. Does anyone know why the following code would not work with that even though it is in my manifest file?
thanks in advance!
There is also a version that I tried using fusedLocationClient, but that doesn't do anything I think that's because my location is not changing.
The goal of this code is to display the current location in terms of latitude and longitude as a toast.
public void displayCurrentLocation(View view){
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Toast.makeText(getApplicationContext(),"Latitude:" + latitude + " Longitude:" + longitude,Toast.LENGTH_SHORT);
// fusedLocationClient.getLastLocation()
// .addOnSuccessListener(this, new OnSuccessListener<Location>() {
// @Override
// public void onSuccess(Location location) {
// double longitude = location.getLongitude();
// double latitude = location.getLatitude();
// Toast.makeText(getApplicationContext(),"Latitude:" + latitude + " Longitude:" + longitude,Toast.LENGTH_SHORT);
// if (location != null) {
// // Logic to handle location object
// }
// }
// });
}