I have created an Android app to retrieve the location. I tried to use GPS_PROVIDER, it returned null.. But works smooth when i use NETWORK_PROVIDER.. Can any one help me fixing this?
Here is the code
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
The location turns null when i replace "NETWORK_PROVIDER"
by "GPS_PROVIDER"
..
I have added the following three permissions in manifest
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
There can be two reasons why your GPS location is null
setting menu => Location & Security => Check Use GPS Satellites
. As per documentation
If the provider is currently disabled, null is returned.
Ensure your GPS is active on the device so you don't receive null.
Edit:
Also try running requestLocationUpdates()
with GPS_PROVIDER
or maybe twice, with NETWORK_PROVIDER
as well. The LocationManager
retrieves the location the fastest way and it stops searching. Since GPS location is retrieved slower than the network location, is stops right after if finds it therefore returning null for the GPS.
The location turns null when i replace "NETWORK_PROVIDER" by "GPS_PROVIDER"
To solve this issue be sure to have enabled the Location services :
remember the docs:
If the provider is currently disabled, null is returned.
As an option you can use both providers if GPS is disabled then check network provider:
...
myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(myLocation == null) {
myLocation = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if(myLocation != null) {
//Get values for latitude and longitude!
}
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.