Skip to content

Instantly share code, notes, and snippets.

@tusharuit25
Created May 29, 2019 15:20
Show Gist options
  • Save tusharuit25/239c022cb4f48cb90410d5fd81d0550c to your computer and use it in GitHub Desktop.
Save tusharuit25/239c022cb4f48cb90410d5fd81d0550c to your computer and use it in GitHub Desktop.
Getting GPS Location Coordinates in One Shot
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;
public class SingleShotLocationProvider {
public interface LocationCallback {
void onNewLocationAvailable(Location location);
}
// calls back to calling thread, note this is for low grain: if you want higher precision, swap the
// contents of the else and if. Also be sure to check gps permission/settings are allowed.
// call usually takes <10ms
public static void requestSingleUpdate(final Context context, final LocationCallback callback) {
final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (Utility.checkAndRequestPermissionsForGPS(context)) {
if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context, "First enable LOCATION ACCESS in settings.", Toast.LENGTH_LONG).show();
return;
}
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGPSEnabled) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
locationManager.requestSingleUpdate(criteria, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
callback.onNewLocationAvailable(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}, null);
} else {
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isNetworkEnabled) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
locationManager.requestSingleUpdate(criteria, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
callback.onNewLocationAvailable(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}, null);
}
}
}
}
}
// in your file where you looking for coordinates
import com.yourdomain.utils.SingleShotLocationProvider;
// somewhere you are pushing buttonor any event is happening
SingleShotLocationProvider.requestSingleUpdate(this,
new SingleShotLocationProvider.LocationCallback() {
@Override
public void onNewLocationAvailable(Location location) {
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment