I'm using android 4.4.2 and I'm trying to get the device location coordinates without success.
This is the code I'm using :
On Manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
On class:
private double[] getLastBestLocation() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
/* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}
double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
return gps;
}
I've tried different answers provided at SO, specifically:
How do I get the current GPS location programmatically in Android?
How to get Android GPS location
Please don't mark this question as duplicated since other answers didn't solve my problem.