I know this has been asked a few times but I could not find a solution that allows you to set how long to wait.
Here is the code I use to retrieve users current location via GPS.
/**
* Finds users location using gps
* satelite network.
*
* @param FragmentActivity
* @return Location
*/
private static Location gpsLocation(FragmentActivity context)
{
// Fetch LocationManager service & instance of UserLocator
LocationManager provider = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
UserLocator locator = new UserLocator();
// Attempt to get a fix on users location
provider.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locator);
// Return users location using GPS Service
return locator.location;
}
/**
* Use to listen for user location updates
* TODO: Reimplement as a anonymous class
*/
class UserLocator implements LocationListener
{
// Public accessor
public Location location;
public void onLocationChanged(Location location)
{
if(location != null)
{
// We have a fix on users location
this.location = location;
}
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
Now I would like to actually give the gpsLocation()
method 30 seconds to fetch the users location but I don't know how I would do this possibly using wait(100) or a for loop.