I am trying to retrieve the my current location coordinates in the following piece of code
LocationManager locationManager= (LocationManager) getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
I get the location coordinates when the provider is "network".
When I change the settings in the location and security options to enable "Use GPS satellites" option the provider changes to "gps". In this case the location returned is null.
What could be the cause of this and how can it be rectified?
It takes a while for the GPS provider to get a fix, especially if no previous fix was made. The getLastKnownLocation is not a blocking call that will wait untill an actual live GPS fix is established (that fix can take up to 1 min, depending on various reasons).
Also, make sure you're outside for obvious reasons).
Instead of simply calling the getLastKnownLocation for the GPS provider, you first need to request location updates from it.
The following article explains the principle very well: http://developer.android.com/guide/topics/location/obtaining-user-location.html
It's a must read for anyone doing anything with the location provider on Android.
the typical flow is:
Looking at the code below, we’re initializing a MyLocationListener (to track the phone’s position), and retrieving a reference to the LocationManager. We’re requesting location updates from the locationmanager. We’re using a minDistance (minimum distance interval for notifications) of 10 meters, and a minTime (the minimum time interval for notifications) of 35 seconds.
LocationListener locationListener = new MyLocationListener();
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locationListener);
When implementing this, you'll probably notice that the MyLocationListener receives more updates than you expected (given the minTime and minDistance parameters). The following article at http://blog.doityourselfandroid.com/2010/12/25/understanding-locationlistener-android/ can help you understand why.
I think the problem is that GPS take a fiew second to syncronice. You must to use a thread. Take this example:
@Override
public void run() {
mLocationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Looper.prepare();
mToast.Make(getContext(),"GPS",0);
mLocationListener = new MyLocationListener();
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
Looper.loop();
Looper.myLooper().quit();
} else if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
Looper.prepare();
mToast.Make(getContext(),"Triangulacion",0);
mLocationListener = new MyLocationListener();
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListener);
Looper.loop();
Looper.myLooper().quit();
}else{
mToast.Make(context,"No se encuentra señal se procede a mandar un mensaje normal",0);
Looper.prepare();
handlerNormal.sendEmptyMessage(0);
Looper.loop();
Looper.myLooper().quit();
}
}
and add a Location Listener class
private class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc) {
if (loc != null) {
setCurrentLocation(loc);
handler.sendEmptyMessage(0);
}
}
//...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With