FusedLocationProviderClient not returning location - android

I am using FusedLocationProviderClient to get latitude and longitude on a button click. But when I click the button it shows nothing. Just the app keeps loading for eternity. Can any show me where I am doing wrong in the code?
This is the code:
public class MainActivity extends AppCompatActivity {
Button showLocation;
TextView getLat, getLong;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showLocation = findViewById(R.id.btnShowLocation);
getLat = findViewById(R.id.getLat);
getLong = findViewById(R.id.getLong);
}
public void getlocation(View v) {
final ProgressDialog progressDialog=new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Getting Location");
progressDialog.setCancelable(false);
progressDialog.show();
FusedLocationProviderClient fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(MainActivity.this);
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Task<Location> task = fusedLocationProviderClient.getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
if(location!=null)
{
String lati=String.valueOf(location.getLatitude());
getLat.setText(lati);
String longi=String.valueOf(location.getLongitude());
getLat.setText(longi);
Toast.makeText(MainActivity.this,"Location Found !!!",Toast.LENGTH_LONG).show();
progressDialog.cancel();
}
else
Toast.makeText(MainActivity.this,"Please Enable GPS And Internet !!!",Toast.LENGTH_LONG).show();
}
});
}
}

I have used FusedLocation in one of my Kotlin project , i will post the method below.
Make sure that , your location service is enable also the permission for accessing location is already asked from user, else last location cannot be detected.
fun getLocation(): Task<Location>? {
val mFusedLocationProviderClient: FusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(dashboardActivity)
if (ActivityCompat.checkSelfPermission(dashboardActivity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(dashboardActivity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
}
val location = mFusedLocationProviderClient.lastLocation
location.addOnCompleteListener { task ->
if (task.isSuccessful) {
val currentLocation = task.result
if (currentLocation != null) {
latitude = currentLocation.latitude
longitude = currentLocation.longitude
dashboardActivity.getLatLong(latitude.toString(), longitude.toString()) //this methods calls the collector geo-location service
} else {
val builder = AlertDialog.Builder(dashboardActivity)
builder.setTitle("Location On")
builder.setPositiveButton("Yes") { dialog, which ->
val viewIntent = Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)
dashboardActivity.startActivity(viewIntent)
}
builder.setNegativeButton("No") { dialog, which ->
Toast.makeText(dashboardActivity, "Please enable location service", Toast.LENGTH_SHORT).show()
}
val dialog: AlertDialog = builder.create()
dialog.show()
}
}
}
return location
}
Instead of using addOnSuccessListener() i have instead used addOnCompleteListener() , i think that might be the issue.

Related

How to ask every time for permission on activity start if the permission is not granted or denied previously

I am working on an android app that needs user location, every time user logins into the app.
I have written code to get user location and works fine, but the problem is if the user denies permission twice it didn't ask again, here is my code:
public class MainActivity extends AppCompatActivity {
String myLocation = "";
private LocationRequest locationRequest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(5000);
locationRequest.setFastestInterval(2000);
getCurrentLocation();
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (isGPSEnabled()) {
getCurrentLocation();
} else {
turnOnGPS();
}
}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2) {
if (resultCode == Activity.RESULT_OK) {
getCurrentLocation();
}
}
}
private void getCurrentLocation() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if (isGPSEnabled()) {
LocationServices.getFusedLocationProviderClient(MainActivity.this)
.requestLocationUpdates(locationRequest, new LocationCallback() {
#Override
public void onLocationResult(#NonNull LocationResult locationResult) {
super.onLocationResult(locationResult);
LocationServices.getFusedLocationProviderClient(MainActivity.this)
.removeLocationUpdates(this);
if (locationResult != null && locationResult.getLocations().size() > 0) {
int index = locationResult.getLocations().size() - 1;
double latitude = locationResult.getLocations().get(index).getLatitude();
double longitude = locationResult.getLocations().get(index).getLongitude();
myLocation = "Latitude: " + latitude + " Longitude: " + longitude;
Toast.makeText(getApplicationContext(), myLocation, Toast.LENGTH_LONG).show();
}
}
}, Looper.getMainLooper());
} else {
turnOnGPS();
}
} else {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
}
}
private void turnOnGPS() {
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(getApplicationContext())
.checkLocationSettings(builder.build());
result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
#Override
public void onComplete(#NonNull Task<LocationSettingsResponse> task) {
try {
LocationSettingsResponse response = task.getResult(ApiException.class);
Toast.makeText(MainActivity.this, "GPS is already tured on", Toast.LENGTH_SHORT).show();
} catch (ApiException e) {
switch (e.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
ResolvableApiException resolvableApiException = (ResolvableApiException) e;
resolvableApiException.startResolutionForResult(MainActivity.this, 2);
} catch (IntentSender.SendIntentException ex) {
ex.printStackTrace();
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
//Device does not have location
break;
}
}
}
});
}
private boolean isGPSEnabled() {
LocationManager locationManager = null;
boolean isEnabled = false;
if (locationManager == null) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
isEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
return isEnabled;
}
}
As, the user's location is mandatory for the working of app. Now, I want the app to:
1- ask for permission on activity start, if the permission is not granted or denied previously.
2- if permission is granted then proceed further else close the activity.
So, how can I achieve this.
Thanks
You can check whether the user has given permission or not using checkSelfPermission and if not then you ask for permission again else process with program flow.
if (ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED){
doStuff()
} else {
requestStoragePermission()
}
but the problem is if the user denies permission twice it didn't ask
again,
Yes, you cannot ask again once the user denies the permission with "Don't ask again" checked. This checkbox is normally checked by default on the second permission request. Beware that on api 11, there is no checkbox and the denial is automatic auto-denial auto-reset.
The way is that, you don't assume something about it, just put logic for handling it. If denied, you can only inform the user about the required permission. The official doc says: ref
If the ContextCompat.checkSelfPermission() method returns
PERMISSION_DENIED, call shouldShowRequestPermissionRationale(). If
this method returns true, show an educational UI to the user. In this
UI, describe why the feature, which the user wants to enable, needs a
particular permission.
info

last known location is always 0 [duplicate]

This question already has answers here:
Location is empty at the start
(5 answers)
Closed 3 years ago.
Inspite of my best effort, I am unable to get my location correct. This is my main.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
//new SecondFragment();
//TabLayout tabs = findViewById(R.id.tabs);
//tabs.setupWithViewPager(viewPager);
FloatingActionButton fab = findViewById(R.id.fab);
FontDrawable drawable = new FontDrawable(this, R.string.fa_plus_solid, true, false);
drawable.setTextColor(ContextCompat.getColor(this, android.R.color.white));
fab.setImageDrawable(drawable);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
//Check permission
if (ContextCompat.checkSelfPermission(getApplicationContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(getApplicationContext(),
android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
latlang.Lat =location.getLatitude();
latlang.Lang =location.getLongitude();
}
}
});
}
}
}
the latlang is defined in another file as:
public class latlang {
public static double Lat;
public static double Lang;
}
If I hardcode the value here in latlang, everything is fine. But, of course I want the value of last known location.
Kindly help.
The problem is that you try to get the location before the permission is granted.
After calling requestPermissions() you immediately try to get location while the permission dialog is being displayed which means that the code that gets the location is being executed while you are seeing the permission dialog. This is why there is onRequestPermissionsResult function in activity that reports only after the user is done with permissions dialog.
You will need to get the location in onRequestPermissionsResult().
Second problem in your code is that you are only getting location when the permission is not granted. Your code does not handle the case when permission is already granted.
Here is how you would solve this:
#Override
protected void onCreate(Bundle savedInstanceState) {
if (ContextCompat.checkSelfPermission(getApplicationContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(getApplicationContext(),
android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[] {
android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION
}, 101);
} else {
fetchLocation();
}
}
#Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions, int[] grantResults) {
switch (requestCode) {
case 101:
{
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 &&
(grantResults[0] == PackageManager.PERMISSION_GRANTED ||
grantResults[1] == PackageManager.PERMISSION_GRANTED)) {
// permission was granted
fetchLocation()
} else {
Show some error
}
return;
}
// other 'case' lines to check for other
// permissions this app might request.
}
}
public void fetchLocation() {
FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener < Location > () {
#Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
latlang.Lat = location.getLatitude();
latlang.Lang = location.getLongitude();
}
}
});
}
Basically, Create a separate function that gets the location. You will call this function from 2 places. First is in onCreate when the permission is granted, and second in onPermissionResult. Don't forget to handle permission errors.

request location updates never called onLocationResult

I am using FusedLocationProvider to get the last location of the phone. Unfortunately the returned location is far from being accurate and it is not updating!.
Even When I call requestlocationupdates,the OnlocationResult never being called with PRIORITY_HIGH_ACCURACY. BUT it called ONLY once with PRIORITY_BALANCED_POWER_ACCURACY.
protected void startLocationUpdates() {
// Create the location request to start receiving updates
locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
locationRequest.setInterval(5000);
locationRequest.setFastestInterval(1000);
// Create LocationSettingsRequest object using location request
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(locationRequest);
LocationSettingsRequest locationSettingsRequest = builder.build();
// Check whether location settings are satisfied
// https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient
SettingsClient settingsClient = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task =settingsClient.checkLocationSettings(locationSettingsRequest);
task.addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {
#Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
// All location settings are satisfied. The client can initialize
// location requests here.
// ...
Log.i("SRV", "onSuccess: Location settings are satisfied");
}
});
task.addOnFailureListener( new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
if (e instanceof ResolvableApiException) {
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
Log.i("SRV", "onFailure: ");
}
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// Activity#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
initialized = false;
//return;
}
}
// new Google API SDK v11 uses getFusedLocationProviderClient(this)
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mFusedLocationClient.requestLocationUpdates(locationRequest, new LocationCallback() {
#Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
Log.i("SRV", "onLocationResult is called ");
if (locationResult == null) {
return;
}
for (Location location : locationResult.getLocations()) {
if (location != null) {
Log.i("SRV", "My new location is " + location.getLatitude() + " " + location.getLongitude());
onLocationChanged(location);
}
}
}
#Override
public void onLocationAvailability(LocationAvailability locationAvailability) {
Log.i("SRV", "onLocationAvailability is called ");
}
},
Looper.myLooper());
//get last location
mFusedLocationClient.getLastLocation().addOnSuccessListener(location -> {
if (location != null) {
_phoneLocation = location;
Log.i("SRV", "getLastLocation is called : My position " + location.getLatitude() + " " + location.getLongitude());
}
});
Log.i("SRV", "Startlocation updated was completed succesfully");
}
I expect the OnlocationResult to be called and updated very 5 seconds.
It turns out that it is a stupid thing. We spent too much time on this. I had some issues of checking the permissions and allowing it.
Here I post the final code that works perfectly. Make sure to allow the location permission when it prompts. If you are debugging many times, It is recommended to uninstall the app first to allow the permission again.
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION},
1);
} else if (mMap != null) {
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
}

Unable to update current location FusedLocationClient

I am using fused location provider for fetching current location and update my marker to my current location via that. I am giving app permission and then it does nothing after that. Though i have written code for that to fetch and update location on map. I have previously given Access_COARSE_LOCATION as well, but nothing changes. this is my code :-
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private FusedLocationProviderClient mFusedLocationClient;
private static final int LOCATION_PERMISSION_REQUEST_CODE = 537;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
// Initializing Objects...
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
if (mapFragment != null) {
mapFragment.getMapAsync(this);
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
// onMapReady check Permission and load map...
// If Permission not granted ask for permission...
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap = googleMap;
MoveCameraToCurrentPosition();
} else {
AskUserPermission();
}
}
public void AskUserPermission() {
// Show Explanation and show permission dialog...
// Permission is not granted
// Show Explanation...
// shouldShowRequestPermissionRationale returns true if permission has previously
// denied by user..
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Why we need Location Permission?");
builder.setMessage("we want to forecast the weather alert like storms, Flood," +
"Hurricanes, etc. to you before hand based on your farm location, so that " +
"we can save your crops :).");
builder.setNeutralButton("Got it.", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
RequestLocationPermission();
}
});
builder.show();
RequestLocationPermission();
} else {
// No Explanation Needed Request Permission...
RequestLocationPermission();
}
}
// Show Alert Dialog for permission...
private void RequestLocationPermission() {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission
.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE);
}
// Fetch Request Permission result and update current location...
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case LOCATION_PERMISSION_REQUEST_CODE:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// This code is not executing even after giving permission..
MoveCameraToCurrentPosition();
}
break;
}
}
// Move camera to current location...
private void MoveCameraToCurrentPosition(){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Add a marker in Sydney, Australia, and move the camera.
LatLng sydney = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(sydney).icon(BitmapDescriptorFactory.fromResource(R.drawable.barley)).title("Your Location"));
CameraPosition cameraPosition = new CameraPosition(sydney, 10, 0, 0);
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
mMap.animateCamera(cameraUpdate);
}
}
});
} else {
AskUserPermission();
}
}
}
Okay so my problem was i was getting not calling getMapAsync after i allow location permission, which was making my Google Map Object null and so that was not ablw to move my camera to my current location. I have updated my code and now it works fine.
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap googleMap;
private String TAG = this.getClass().getSimpleName();
private FusedLocationProviderClient mFusedLocationClient;
private SupportMapFragment mapFragment;
private static final int LOCATION_PERMISSION_REQUEST_CODE = 537;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
if (mapFragment != null) {
mapFragment.getMapAsync(this);
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
if (googleMap != null) {
this.googleMap = googleMap;
MoveCameraToCurrentPosition();
} else {
Toast.makeText(this, "Null Object...OnMapReady", Toast.LENGTH_SHORT).show();
}
} else {
AskUserPermission();
}
}
public void AskUserPermission() {
// Show Explanation and show permission dialog...
// Permission is not granted
// Show Explanation...
// shouldShowRequestPermissionRationale returns true if permission has previously
// denied by user..
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Why we need Location Permission?");
builder.setMessage("we want to forecast the weather alert like storms, Flood," +
"Hurricanes, etc. to you before hand based on your farm location, so that " +
"we can save your crops :).");
builder.setNeutralButton("Got it.", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
builder.show();
}
RequestLocationPermission();
}
private void RequestLocationPermission() {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission
.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case LOCATION_PERMISSION_REQUEST_CODE:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (googleMap != null) {
MoveCameraToCurrentPosition();
} else {
if (mapFragment != null) {
mapFragment.getMapAsync(this);
}
}
}
break;
}
}
private void MoveCameraToCurrentPosition() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Add a marker in Sydney, Australia, and move the camera.
LatLng sydney = new LatLng(location.getLatitude(), location.getLongitude());
googleMap.addMarker(new MarkerOptions().position(sydney).icon
(BitmapDescriptorFactory.fromResource(R.drawable.barley)).title("Your Location"));
CameraPosition cameraPosition = new CameraPosition(sydney, 10, 0, 0);
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
googleMap.animateCamera(cameraUpdate);
}
}
});
} else {
AskUserPermission();
}
}
}

FusedLocationApi is deprecated [duplicate]

I want to get periodic (say every 2 minutes) current location updates for this I'm following official documentation, I wrote this code but its not giving current location updates every two minutes even specified in LocationRequest object that I'm passing in requestLocationUpdates(), here is code:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private FusedLocationProviderClient FusedLocationClient;
private GoogleApiClient mGoogleApiClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
SupportMapFragment map =
getSupportFragmentManager().findFragmentById(R.id.map));
map.getMapAsync(this);
FusedLocationClient LocationServices.getFusedLocationProviderClient(this);
}
#Override
public void onConnected(Bundle bundle) {
FusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
if (location != null) {
Log.i("MainActivity ", "" + location.getLongitude())
}
}
});
FusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
FusedLocationClient.requestLocationUpdates(requestLocation(),
new LocationCallback(){
#Override
public void onLocationResult(LocationResult locationResult) {
for (Location location : locationResult.getLocations()) {
Log.i("MainActivity ", "" + location.getLongitude());
//not getting current location updates every 2 minutes
}
};
},null);
}
#Override
public void onConnectionSuspended(int i) {}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {}
This is similar to my other answer here, updated to use the recently introduced FusedLocationProviderClient class.
In order to use a FusedLocationProviderClient in conjunction with a Google Map:
Wait until the Google Map is ready
Request the Location permission at runtime if needed
Request location updates once the permission is granted
Update the Google Map once the user's location is obtained
First ensure that you're using at least version 11 of Google Play Services, as older versions don't have the FusedLocationProviderClient class (newer versions will work as well):
dependencies {
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'
//........
}
Note that the FusedLocationProviderClient is present in version 11.0.2, but due to bugs in the initial implementation, it's recommended that you only use this class on 11.6.0 and later.
From the documentation:
Note: It's recommended to use Google Play services version 11.6.0 or
higher, which includes bug fixes for this class.
Add the location permissions in the AndroidManifest.xml file, inside the manifest tag and outside of the application tag:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Add the API key to the AndroidManifest.xml inside the application tag:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIza___YOUR_KEY_HERE______"/>
Kotlin
Here is the full Activity class in Kotlin:
class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
lateinit var mGoogleMap: GoogleMap
var mapFrag: SupportMapFragment? = null
lateinit var mLocationRequest: LocationRequest
var mLastLocation: Location? = null
internal var mCurrLocationMarker: Marker? = null
internal var mFusedLocationClient: FusedLocationProviderClient? = null
internal var mLocationCallback: LocationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
val locationList = locationResult.locations
if (locationList.isNotEmpty()) {
//The last location in the list is the newest
val location = locationList.last()
Log.i("MapsActivity", "Location: " + location.getLatitude() + " " + location.getLongitude())
mLastLocation = location
if (mCurrLocationMarker != null) {
mCurrLocationMarker?.remove()
}
//Place current location marker
val latLng = LatLng(location.latitude, location.longitude)
val markerOptions = MarkerOptions()
markerOptions.position(latLng)
markerOptions.title("Current Position")
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA))
mCurrLocationMarker = mGoogleMap.addMarker(markerOptions)
//move map camera
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 11.0F))
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
supportActionBar?.title = "Map Location Activity"
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
mapFrag = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
mapFrag?.getMapAsync(this)
}
public override fun onPause() {
super.onPause()
//stop location updates when Activity is no longer active
mFusedLocationClient?.removeLocationUpdates(mLocationCallback)
}
override fun onMapReady(googleMap: GoogleMap) {
mGoogleMap = googleMap
mGoogleMap.mapType = GoogleMap.MAP_TYPE_HYBRID
mLocationRequest = LocationRequest()
mLocationRequest.interval = 120000 // two minute interval
mLocationRequest.fastestInterval = 120000
mLocationRequest.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED
) {
//Location Permission already granted
mFusedLocationClient?.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper())
mGoogleMap.isMyLocationEnabled = true
} else {
//Request Location Permission
checkLocationPermission()
}
} else {
mFusedLocationClient?.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper())
mGoogleMap.isMyLocationEnabled = true
}
}
private fun checkLocationPermission() {
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(
this,
Manifest.permission.ACCESS_FINE_LOCATION
)
) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
AlertDialog.Builder(this)
.setTitle("Location Permission Needed")
.setMessage("This app needs the Location permission, please accept to use location functionality")
.setPositiveButton(
"OK"
) { _, _ ->
//Prompt the user once explanation has been shown
ActivityCompat.requestPermissions(
this#MapsActivity,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
MY_PERMISSIONS_REQUEST_LOCATION
)
}
.create()
.show()
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
MY_PERMISSIONS_REQUEST_LOCATION
)
}
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>, grantResults: IntArray
) {
when (requestCode) {
MY_PERMISSIONS_REQUEST_LOCATION -> {
// If request is cancelled, the result arrays are empty.
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// location-related task you need to do.
if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED
) {
mFusedLocationClient?.requestLocationUpdates(
mLocationRequest,
mLocationCallback,
Looper.myLooper()
)
mGoogleMap.setMyLocationEnabled(true)
}
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show()
}
return
}
}// other 'case' lines to check for other
// permissions this app might request
}
companion object {
val MY_PERMISSIONS_REQUEST_LOCATION = 99
}
}
Java
Here is the full Activity class in Java:
public class MapsActivity extends AppCompatActivity
implements OnMapReadyCallback {
GoogleMap mGoogleMap;
SupportMapFragment mapFrag;
LocationRequest mLocationRequest;
Location mLastLocation;
Marker mCurrLocationMarker;
FusedLocationProviderClient mFusedLocationClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
getSupportActionBar().setTitle("Map Location Activity");
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFrag.getMapAsync(this);
}
#Override
public void onPause() {
super.onPause();
//stop location updates when Activity is no longer active
if (mFusedLocationClient != null) {
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(120000); // two minute interval
mLocationRequest.setFastestInterval(120000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
//Location Permission already granted
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
mGoogleMap.setMyLocationEnabled(true);
} else {
//Request Location Permission
checkLocationPermission();
}
}
else {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
mGoogleMap.setMyLocationEnabled(true);
}
}
LocationCallback mLocationCallback = new LocationCallback() {
#Override
public void onLocationResult(LocationResult locationResult) {
List<Location> locationList = locationResult.getLocations();
if (locationList.size() > 0) {
//The last location in the list is the newest
Location location = locationList.get(locationList.size() - 1);
Log.i("MapsActivity", "Location: " + location.getLatitude() + " " + location.getLongitude());
mLastLocation = location;
if (mCurrLocationMarker != null) {
mCurrLocationMarker.remove();
}
//Place current location marker
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Current Position");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
//move map camera
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 11));
}
}
};
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
private void checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
new AlertDialog.Builder(this)
.setTitle("Location Permission Needed")
.setMessage("This app needs the Location permission, please accept to use location functionality")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//Prompt the user once explanation has been shown
ActivityCompat.requestPermissions(MapsActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION );
}
})
.create()
.show();
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION );
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// location-related task you need to do.
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
mGoogleMap.setMyLocationEnabled(true);
}
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
}
activity_maps.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".map.MapsActivity">
<fragment android:id="#+id/map"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".map.MapsActivity"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</androidx.constraintlayout.widget.ConstraintLayout>
The user will be prompted to accept the Location permission:
The location will be updated on app launch, and every two minutes:
Extra note regarding AndroidX
If you are using AndroidX, you might need to add these lines to your gradle.properties file (see here for more info):
android.useAndroidX=true
android.enableJetifier=true
mLocationCallback = new LocationCallback() {
#Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
currentLocation = locationResult.getLastLocation();
}
};
this work for me.
For get current location. use below code .
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//Please allow Location Permission
return;
}
FusedLocationProviderClient fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
fusedLocationProviderClient.getCurrentLocation(PRIORITY_HIGH_ACCURACY, new CancellationToken() {
#Override
public boolean isCancellationRequested() {
return false;
}
#NonNull
#Override
public CancellationToken onCanceledRequested(#NonNull OnTokenCanceledListener onTokenCanceledListener) {
return null;
}
}).addOnSuccessListener(location -> {
currentLocation = location;
// use this current location
});
private LocationRequest locationRequest;
public class MapsActivity extends FragmentActivity implements LocationListener{
locationRequest = new LocationRequest();
locationRequest.setInterval(60 * 1000);
locationRequest.setFastestInterval(15 * 1000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
#Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
Implement Location change listener and you will be able to override onlocation changed...
For make it simple, try with this library https://github.com/mrmans0n/smart-location-lib. This will use the Fused Location Provider.
You just put this code
SmartLocation.with(context).location(new LocationBasedOnActivityProvider(callback))
.start(new OnLocationUpdatedListener() { ... });

Categories

Resources