Created
April 9, 2016 04:40
-
-
Save MinaGabriel/8f769789709fa2c3e1d1bb42a9a2c4a7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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