0

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.

Sterling Duchess
  • 1,970
  • 16
  • 51
  • 91
  • Using a `wait(100)` will probably block your `Thread`, so doing it isn't really a good idea. Same with a long for loop. – Aleksander Lidtke Nov 03 '13 at 17:08
  • Besides 30 seconds is probably too little. Way too little. – Aleksander Lidtke Nov 03 '13 at 17:25
  • 30s Is not to little. It's not the best but GPS is not crucial to my application I want to give it 30s to establish lock if it does then it will zoom in on users position otherwise it will simply load the map with no zoom. – Sterling Duchess Nov 03 '13 at 17:32
  • It takes minutes to receive and decode the full GPS navigational message. So it is too little. If that's all you need it for I'd try all `LocationProviders`. – Aleksander Lidtke Nov 03 '13 at 18:23
  • See my answer to: http://stackoverflow.com/questions/19365035/location-servise-gps-force-closed/19366773#19366773 It is a GPS implementation that has a callback as soon as a GPS first-fix has been made. Please give a +1 if it helps you. – cYrixmorten Nov 03 '13 at 18:45
  • @cYrixmorten Lots of code there Ill look it over again see if I can find what I need from it. – Sterling Duchess Nov 03 '13 at 18:53
  • You can copy the whole class and use it as described in the upmost section – cYrixmorten Nov 03 '13 at 19:20

0 Answers0