Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android GPS takes a while to be accurate

I have made an Android app that gets location by longitude and latitude on a button click.

At first I get the last known location reading, which is, for argument sake, inaccurate when I first load up the app/turn on gps.

What I would like to know is how to wait for it to be accurate, like in Google maps when you get the toast message 'waiting for location'.

If you see any way the code can be improved it would also be helpful.

Code for reference:

public class Clue extends Activity {

    public static double latitude;
    public static double longitude;
    Criteria criteria;
    LocationManager lm;
    LocationListener ll;
    Location location;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.questions);

      criteria = new Criteria();
      criteria.setAccuracy(Criteria.ACCURACY_FINE);
      lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
      ll = new MyLocationListener();
      lm.requestLocationUpdates(lm.getBestProvider(criteria, true), 0, 0, ll);          
}



private boolean weAreThere() {
    location = getLocation(); 
    longitude = location.getLongitude();
    latitude = location.getLatitude();

    return inCorrectPlace(param);
}

private Location getLocation() {
     lm.requestLocationUpdates(lm.getBestProvider(criteria, true), 0, 0, ll); 
      return lm.getLastKnownLocation(lm.getBestProvider(criteria, true));
}
}

    public class MyLocationListener implements LocationListener
    {
        public void onLocationChanged(Location loc)
        {        
             Clue.longitude = loc.getLongitude();
             Clue.latitude = loc.getLatitude();
        }
}

Thanks for reading, all replies will be appreciated.

Ben

like image 378
Ben Taliadoros Avatar asked Feb 23 '23 04:02

Ben Taliadoros


1 Answers

If Location.hasAccuracy() returns true, you could call Location.getAccuracy() to retrieve the accuracy in meters, then filter the ones you don't consider enough accurate.

Notice you are not using LocationManager.GPS_PROVIDER so your fixes could also be obtained by other means (like WiFi).

Usually expect about a minute for the GPS chip to get "hot"* before getting GPS fixes (outdoors).

*By hot I mean having satellites coverage. Some chipsets get disconnected ("cold") after some time to preserve battery. The Time To First Fix (TTFF) is greater when the chip is cold.

like image 194
Mister Smith Avatar answered Feb 28 '23 19:02

Mister Smith