1

I have to wait to get the current location, just like when you are in Google maps and it requests for your location. If it can't get any location inform the error and do nothing.

I read the answers here and in web but all say that I have to use getLastKnownLocation method, but I don't need this, I just need the current location. If not, I don't do anything.

Bart
  • 19,692
  • 7
  • 68
  • 77
user1252306
  • 87
  • 3
  • 15

1 Answers1

1

Read this from yesterday: GPS Android - get positioning only once

But here is how you can get the current location:

public class Example extends Activity implements LocationListener {
    LocationManager mLocationManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

        Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) {
            // Do something with the recent location fix 
            //  if it is less than two minutes old,
            //  otherwise wait for the update below
        }
    }

    public void onLocationChanged(Location location) {
        if (location != null) {
            Log.v("Location Changed", location.getLatitude() + " and " + location.getLongitude());
            // You need to call this whenever you are done:
            // mLocationManager.removeUpdates(this);
        }
    }

    // Required functions    
    public void onProviderDisabled(String arg0) {}
    public void onProviderEnabled(String arg0) {}
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
}
Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179
  • thanks! i will try this. One question, how my method can wait the location update in order to do some stuff. for example save the location in a database. Should i use wait, notify?? – user1252306 May 10 '12 at 17:09
  • I don't understand your question... You can do stuff to each new GPS location when it is given to you in onLocationChanged(). If you only want to check for a new location every five minutes or 500 meters change requestLocationUpdates(). – Sam May 10 '12 at 17:17
  • Sam, for example if i want to store the obtained location in a database, i hace to wait until the GPS get the location, thats what i meant. – user1252306 May 10 '12 at 18:21
  • 1
    Ok, onLocationChanged() is called only when a new location is available and in there you would call something like `db.insert(location.getTime(), location.getLatitude(), location.getLongitude());` to save the location to a database. For help with databases [start here](http://developer.android.com/guide/topics/data/data-storage.html#db) – Sam May 10 '12 at 18:32
  • 1
    Sam is right; start your app, enable GPS, and return. When (and if) location finally becomes available, your onLocationChanged() method is called. Then you do whatever it was you were going to do once you got the location, disable the GPS and you're done. Also: I would set up a Handler and use postDelayed() with a timeout of say ten minutes or so. If the handler gets called before you get a location, it's time to give up because you're not going to get a location. – Edward Falk Oct 02 '12 at 20:26