Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Location returned is null when provider is gps?

Tags:

java

android

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?

like image 864
rogerstone Avatar asked Dec 21 '22 15:12

rogerstone


2 Answers

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:

  1. Start application.
  2. Start listening for updates from desired location providers.
  3. Maintain a "current best estimate" of location by filtering out new, but less accurate fixes.
  4. Stop listening for location updates.
  5. Take advantage of the last best location estimate.

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.

like image 187
ddewaele Avatar answered Jan 05 '23 11:01

ddewaele


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);
        }
    }  
//...  
} 
like image 35
Aracem Avatar answered Jan 05 '23 10:01

Aracem