Skip to content

Instantly share code, notes, and snippets.

@pkavoo
Created April 2, 2020 21:36
Show Gist options
  • Save pkavoo/966e1c6c40d6daf105b2bc47fc525de8 to your computer and use it in GitHub Desktop.
Save pkavoo/966e1c6c40d6daf105b2bc47fc525de8 to your computer and use it in GitHub Desktop.
Here you can change the minimum time or distance to get new locations and draw on map
private LocationManager locationManager;
private android.location.LocationListener myLocationListener;
public void checkLocation() {
String serviceString = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(serviceString);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
myLocationListener = new android.location.LocationListener() {
public void onLocationChanged(Location locationListener) {
if (isGPSEnabled(YourActivityName.this)) {
if (locationListener != null) {
if (ActivityCompat.checkSelfPermission(YourActivityName.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(YourActivityName.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
} else if (isInternetConnected(YourActivityName.this)) {
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, myLocationListener); // here the min time interval and min distance
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment