Java – Android GPS takes a while to be accurate

Android GPS takes a while to be accurate… here is a solution to the problem.

Android GPS takes a while to be accurate

I made an Android app that gets latitude and longitude location information by clicking a button.

At first I got the last known position reading, for reasons of argument it was inaccurate when I first loaded the app/turned on the gps.

What I’m wondering is how to wait for it to be accurate, like when you get a toast message in Google map that says “Waiting for location”.

It would also help if you saw any ways to improve the code.

Quote code:

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. */
  public void onCreate(Bundle savedInstanceState) {

      criteria = new Criteria();
      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 responses would be appreciated.



Location.hasAccuracy() If returns true, you can call Location.getAccuracy() to retrieve the precision in meters and then filter out the precision that you don’t consider to be accurate enough.

Note that you are not using LocationManager.GPS_PROVIDERit, so your fix can also be obtained by other means, such as WiFi.

Before obtaining a GPS fix (outdoors), it is generally expected that the GPS chip will “get hot”* for a minute or so.

*By hotspots, I mean there is satellite coverage. Some chipsets disconnect (“cold”) after a while to preserve the battery. When the chip is cold, the time to first fix (TTFF) is longer.

Related Problems and Solutions