I have seen many posts regarding this problem, but none of them offered a solution for me.
I am trying to get the user position through the GPS (or, ultimately, any other valid provider). But I am always getting null locations. I do get a map, and can set an arbitrary location, is the providers that don't work.
I have my MAPS key properly set (also checked that it was the right one), all necessary permissions set (android.permission.INTERNET
, android.permission.ACCESS_COARSE_LOCATION
, android.permission.ACCESS_FINE_LOCATION
) and in fact, all seemed to be working just fine yesterday!
I tried getting all possible providers with this code:
List<String> providers = locManager.getAllProviders();
for (String provider : providers) {
printProvider(provider);
}
Which shows all providers (4, in my case) are DummyLocationProviders.
I am trying to run my app on the phone, not the emulator (although I also tried with the emulator, with GPS enabled, to no avail).
This is the code I am using to fetch the location (which, again, worked just fine yesterday):
//Get criteria
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
//Get best provider
String bestProvider = locManager.getBestProvider(criteria, false);
printProvider(bestProvider);
//get the current location (last known location) from the location manager
Location location = locManager.getLastKnownLocation(bestProvider);
//if location found display as a toast the current latitude and longitude
if (location != null) {
Toast.makeText(this, "Current location:\nLatitude: " + location.getLatitude() + "\n" + "Longitude: " + location.getLongitude(), Toast.LENGTH_LONG).show();
point = new GeoPoint((int) (location.getLatitude()*1E6),(int)(location.getLongitude() *1E6));
controller.animateTo(point);
} else {
Toast.makeText(this, "Cannot fetch current location!", Toast.LENGTH_LONG).show();
}
My manifest file:
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".activities.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".activities.GoogleMapsActivity"
android:label="@string/title_activity_maps" >
</activity>
<activity
android:name=".activities.MyMapsActivity"
android:label="@string/title_activity_maps" >
</activity>
</application>
</manifest>
//get the current location (last known location) from the location manager Location location = locManager.getLastKnownLocation(bestProvider);
use this instead of above line
First time location is null , so set your current location is last location .
Try this code :
}
Layout Coding :
Link for getting own API key process :
http://sanathnandasiri.blogspot.in/2011/04/obtaining-google-maps-api-key-for.html