Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplest and most robust way to get the user's current location on Android?

The LocationManager API on Android seems like it's a bit of a pain to use for an application that only needs an occasional and rough approximation of the user's location.

The app I'm working on isn't really a location app per se, but it does need to get the user's location in order to display a list of nearby businesses. It doesn't need to worry about if the user is moving around or anything like that.

Here's what I'd like to do:

  1. Show the user a list of nearby locations.
  2. Preload the user's location so that by the time I need it in Activity X, it will be available.
  3. I don't particularly care about accuracy or frequency of update. Just grabbing one location is sufficient as long as it's not way off. Maybe if I want to be fancy I'll update the location once every few mins or so, but it's not a huge priority.
  4. Work for any device as long as it has either a GPS or a Network Location provider.

It seems like it shouldn't be that hard, but it appears to me that I have to spin up two different location providers (GPS and NETWORK) and manage each's lifecycle. Not only that, but I have to duplicate the same code in multiple activities to satisfy #2. I've tried using getBestProvider() in the past to cut the solution down to just using one location provider, but that seems to only give you the best "theoretical" provider rather than the provider that's actually going to give you the best results.

Is there a simpler way to accomplish this?

like image 467
emmby Avatar asked Jun 29 '10 21:06

emmby


People also ask

What is the way to obtain current location in Android?

There are two ways to get the current location of any Android device: Android's Location Manager API. Fused Location Provider: Google Play Services Location APIs.

Which two methods in Android decides the location of the current users using GPS?

Android offers two location permissions: ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION . The permission you choose determines the accuracy of the location returned by the API.


2 Answers

After searching for best implementation how to get best precise user location I managed to combine all the best methods and come up with following class:

/**  * Retrieve accurate location from GPS or network services.   *   *  * Class usage example:  *   * public void onCreate(Bundle savedInstanceState) {  *      ...  *      my_location = new MyLocation();  *      my_location.init(main.this, locationResult);  * }  *   *   * public LocationResult locationResult = new LocationResult(){  *      @Override  *      public void gotLocation(final Location location){  *          // do something  *          location.getLongitude();  *          location.getLatitude();  *      }  *  };  */ class MyLocation{      /**      * If GPS is enabled.       * Use minimal connected satellites count.      */     private static final int min_gps_sat_count = 5;      /**      * Iteration step time.      */     private static final int iteration_timeout_step = 500;      LocationResult locationResult;     private Location bestLocation = null;     private Handler handler = new Handler();     private LocationManager myLocationManager;      public Context context;      private boolean gps_enabled = false;      private int counts    = 0;     private int sat_count = 0;      private Runnable showTime = new Runnable() {               public void run() {             boolean stop = false;             counts++;             System.println("counts=" + counts);                          //if timeout (1 min) exceeded, stop tying             if(counts > 120){                 stop = true;             }                          //update last best location             bestLocation = getLocation(context);                          //if location is not ready or don`t exists, try again             if(bestLocation == null && gps_enabled){                 System.println("BestLocation not ready, continue to wait");                 handler.postDelayed(this, iteration_timeout_step);             }else{                 //if best location is known, calculate if we need to continue to look for better location                 //if gps is enabled and min satellites count has not been connected or min check count is smaller then 4 (2 sec)                   if(stop == false && !needToStop()){                     System.println("Connected " + sat_count + " sattelites. continue waiting..");                     handler.postDelayed(this, iteration_timeout_step);                 }else{                     System.println("#########################################");                     System.println("BestLocation found return result to main. sat_count=" + sat_count);                     System.println("#########################################");                      // removing all updates and listeners                     myLocationManager.removeUpdates(gpsLocationListener);                     myLocationManager.removeUpdates(networkLocationListener);                         myLocationManager.removeGpsStatusListener(gpsStatusListener);                     sat_count = 0;                                          // send best location to locationResult                     locationResult.gotLocation(bestLocation);                 }             }          }     };              /**      * Determine if continue to try to find best location      */     private Boolean needToStop(){          if(!gps_enabled){                           return true;                      }           else if(counts <= 4){                 return false;             }             if(sat_count < min_gps_sat_count){                 //if 20-25 sec and 3 satellites found then stop                 if(counts >= 40 && sat_count >= 3){                     return true;                 }                 return false;             }         }         return true;     }      /**      * Best location abstract result class      */     public static abstract class LocationResult{          public abstract void gotLocation(Location location);      }      /**      * Initialize starting values and starting best location listeners      *       * @param Context ctx      * @param LocationResult result      */     public void init(Context ctx, LocationResult result){         context = ctx;         locationResult = result;              myLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);              gps_enabled = (Boolean) myLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);              bestLocation = null;         counts = 0;              // turning on location updates         myLocationManager.requestLocationUpdates("network", 0, 0, networkLocationListener);         myLocationManager.requestLocationUpdates("gps", 0, 0, gpsLocationListener);         myLocationManager.addGpsStatusListener(gpsStatusListener);              // starting best location finder loop         handler.postDelayed(showTime, iteration_timeout_step);     }      /**      * GpsStatus listener. OnChainged counts connected satellites count.      */     public final GpsStatus.Listener gpsStatusListener = new GpsStatus.Listener() {         public void onGpsStatusChanged(int event) {                       if(event == GpsStatus.GPS_EVENT_SATELLITE_STATUS){                 try {                     // Check number of satellites in list to determine fix state                      GpsStatus status = myLocationManager.getGpsStatus(null);                      Iterable<GpsSatellite>satellites = status.getSatellites();                                            sat_count = 0;                                            Iterator<GpsSatellite>satI = satellites.iterator();                      while(satI.hasNext()) {                          GpsSatellite satellite = satI.next();                          System.println("Satellite: snr=" + satellite.getSnr() + ", elevation=" + satellite.getElevation());                                                   sat_count++;                      }                 } catch (Exception e) {                     e.printStackTrace();                     sat_count = min_gps_sat_count + 1;                 }                                    System.println("#### sat_count = " + sat_count);              }          }     };      /**      * Gps location listener.      */     public final LocationListener gpsLocationListener = new LocationListener(){         @Override          public void onLocationChanged(Location location){                  }          public void onProviderDisabled(String provider){}          public void onProviderEnabled(String provider){}          public void onStatusChanged(String provider, int status, Bundle extras){}     };       /**      * Network location listener.      */     public final LocationListener networkLocationListener = new LocationListener(){         @Override          public void onLocationChanged(Location location){                  }          public void onProviderDisabled(String provider){}          public void onProviderEnabled(String provider){}          public void onStatusChanged(String provider, int status, Bundle extras){}     };        /**      * Returns best location using LocationManager.getBestProvider()      *       * @param context      * @return Location|null      */     public static Location getLocation(Context context){         System.println("getLocation()");              // fetch last known location and update it         try {             LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);                           Criteria criteria = new Criteria();             criteria.setAccuracy(Criteria.ACCURACY_FINE);              criteria.setAltitudeRequired(false);              criteria.setBearingRequired(false);              criteria.setCostAllowed(true);              String strLocationProvider = lm.getBestProvider(criteria, true);                           System.println("strLocationProvider=" + strLocationProvider);              Location location = lm.getLastKnownLocation(strLocationProvider);              if(location != null){                 return location;              }              return null;         } catch (Exception e) {             e.printStackTrace();             return null;         }     } } 

This class tries to connect to min_gps_sat_count satellites if GPS is enabled. Else returns LocationManager.getBestProvider() location. Check the code!

like image 21
wormhit Avatar answered Oct 23 '22 07:10

wormhit


Here's what I do:

  1. First of all I check what providers are enabled. Some may be disabled on the device, some may be disabled in application manifest.
  2. If any provider is available I start location listeners and timeout timer. It's 20 seconds in my example, may not be enough for GPS so you can enlarge it.
  3. If I get update from location listener I use the provided value. I stop listeners and timer.
  4. If I don't get any updates and timer elapses I have to use last known values.
  5. I grab last known values from available providers and choose the most recent of them.

Here's how I use my class:

LocationResult locationResult = new LocationResult(){     @Override     public void gotLocation(Location location){         //Got the location!     } }; MyLocation myLocation = new MyLocation(); myLocation.getLocation(this, locationResult); 

And here's MyLocation class:

import java.util.Timer; import java.util.TimerTask; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle;  public class MyLocation {     Timer timer1;     LocationManager lm;     LocationResult locationResult;     boolean gps_enabled=false;     boolean network_enabled=false;      public boolean getLocation(Context context, LocationResult result)     {         //I use LocationResult callback class to pass location value from MyLocation to user code.         locationResult=result;         if(lm==null)             lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);          //exceptions will be thrown if provider is not permitted.         try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}         try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}          //don't start listeners if no provider is enabled         if(!gps_enabled && !network_enabled)             return false;          if(gps_enabled)             lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);         if(network_enabled)             lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);         timer1=new Timer();         timer1.schedule(new GetLastLocation(), 20000);         return true;     }      LocationListener locationListenerGps = new LocationListener() {         public void onLocationChanged(Location location) {             timer1.cancel();             locationResult.gotLocation(location);             lm.removeUpdates(this);             lm.removeUpdates(locationListenerNetwork);         }         public void onProviderDisabled(String provider) {}         public void onProviderEnabled(String provider) {}         public void onStatusChanged(String provider, int status, Bundle extras) {}     };      LocationListener locationListenerNetwork = new LocationListener() {         public void onLocationChanged(Location location) {             timer1.cancel();             locationResult.gotLocation(location);             lm.removeUpdates(this);             lm.removeUpdates(locationListenerGps);         }         public void onProviderDisabled(String provider) {}         public void onProviderEnabled(String provider) {}         public void onStatusChanged(String provider, int status, Bundle extras) {}     };      class GetLastLocation extends TimerTask {         @Override         public void run() {              lm.removeUpdates(locationListenerGps);              lm.removeUpdates(locationListenerNetwork);               Location net_loc=null, gps_loc=null;              if(gps_enabled)                  gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);              if(network_enabled)                  net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);               //if there are both values use the latest one              if(gps_loc!=null && net_loc!=null){                  if(gps_loc.getTime()>net_loc.getTime())                      locationResult.gotLocation(gps_loc);                  else                      locationResult.gotLocation(net_loc);                  return;              }               if(gps_loc!=null){                  locationResult.gotLocation(gps_loc);                  return;              }              if(net_loc!=null){                  locationResult.gotLocation(net_loc);                  return;              }              locationResult.gotLocation(null);         }     }      public static abstract class LocationResult{         public abstract void gotLocation(Location location);     } } 

Somebody may also want to modify my logic. For example if you get update from Network provider don't stop listeners but continue waiting. GPS gives more accurate data so it's worth waiting for it. If timer elapses and you've got update from Network but not from GPS then you can use value provided from Network.

One more approach is to use LocationClient http://developer.android.com/training/location/retrieve-current.html. But it requires Google Play Services apk to be installed on user device.

like image 182
Fedor Avatar answered Oct 23 '22 08:10

Fedor