Skip to content

Instantly share code, notes, and snippets.

@MinaGabriel
Created April 9, 2016 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MinaGabriel/8f769789709fa2c3e1d1bb42a9a2c4a7 to your computer and use it in GitHub Desktop.
Save MinaGabriel/8f769789709fa2c3e1d1bb42a9a2c4a7 to your computer and use it in GitHub Desktop.
public void getCurrentLocation() {
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(context);
Criteria crta = new Criteria();
crta.setAccuracy(Criteria.ACCURACY_FINE);
crta.setAltitudeRequired(false);
crta.setBearingRequired(false);
crta.setCostAllowed(true);
crta.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(crta, true);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(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;
}
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.d("Hello", String.valueOf(location.getLongitude()));
locationManager.requestLocationUpdates(provider, 1000, 0,
new LocationListener() {
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
if (lat != 0.0 && lng != 0.0) {
System.out.println("WE GOT THE LOCATION");
System.out.println(lat);
System.out.println(lng);
Log.d("Hello" , String.valueOf(lat));
}
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment