SHARE
TWEET

Android Location Helper Class

ScottHelme Dec 8th, 2012 2,520 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import android.content.Context;
  2. import android.location.Location;
  3. import android.location.LocationListener;
  4. import android.location.LocationManager;
  5. import android.os.Bundle;
  6.  
  7. /***
  8.  * Location Helper Class.
  9.  * Handles creation of the Location Manager and Location Listener.
  10.  *
  11.  * @author Scott Helme
  12.  */
  13. public class LocationHelper{
  14.        
  15.         //variables to store lat and long
  16.         private float latitude  = 0.0f;
  17.         private float longitude = 0.0f;
  18.        
  19.         //flag for when we have co-ords
  20.         private boolean gotLocation = false;
  21.        
  22.         //my location manager and listener
  23.         private LocationManager    locationManager;
  24.         private MyLocationListener locationListener;
  25.        
  26.         /**
  27.          * Constructor.
  28.          *
  29.          * @param context - The context of the calling activity.
  30.          */
  31.     public LocationHelper(Context context){
  32.  
  33.         //setup the location manager
  34.         locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
  35.         //create the location listener
  36.         locationListener = new MyLocationListener();
  37.  
  38.         //setup a callback for when the GRPS/WiFi gets a lock and we receive data
  39.         locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
  40.         //setup a callback for when the GPS gets a lock and we receive data
  41.         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
  42.     }
  43.        
  44.     /***
  45.      * Used to receive notifications from the Location Manager when they are sent. These methods are called when
  46.      * the Location Manager is registered with the Location Service and a state changes.
  47.      *
  48.      * @author Scott Helme
  49.      */
  50.         public class MyLocationListener implements LocationListener {
  51.                
  52.                 //called when the location service reports a change in location
  53.                 public void onLocationChanged(Location location) {
  54.                        
  55.                         //store lat and long
  56.                         latitude = (float) location.getLatitude();
  57.                         longitude = (float) location.getLongitude();
  58.  
  59.                         //now we have our location we can stop the service from sending updates
  60.                         //comment out this line if you want the service to continue updating the users location
  61.                         locationManager.removeUpdates(locationListener);
  62.                        
  63.                         //change the flag to indicate we now have a location
  64.                         gotLocation = true;
  65.                 }
  66.  
  67.                 //called when the provider is disabled
  68.                 public void onProviderDisabled(String provider) {}
  69.                 //called when the provider is enabled
  70.                 public void onProviderEnabled(String provider) {}
  71.                 //called when the provider changes state
  72.                 public void onStatusChanged(String provider, int status, Bundle extras) {}
  73.         }
  74.        
  75.         /***
  76.          * Stop updates from the Location Service.
  77.          */
  78.         public void killLocationServices(){
  79.                 locationManager.removeUpdates(locationListener);
  80.         }
  81.        
  82.         /***
  83.          * Get Latitude from GPS Helper.
  84.          * Should check gotLocation() first.
  85.          * @return - The current Latitude.
  86.          */
  87.         public float getLat(){
  88.                 return latitude;
  89.         }
  90.        
  91.         /***
  92.          * Get Longitude from GPS Helper.
  93.          * Should check gotLocation() first.
  94.          * @return - The current Longitude.
  95.          */
  96.         public float getLong(){
  97.                 return longitude;
  98.         }
  99.        
  100.         /***
  101.          * Check if a location has been found yet.
  102.          * @return - True if a location has been acquired. False otherwise.
  103.          */
  104.         public Boolean gotLocation(){
  105.                 return gotLocation;
  106.         }      
  107. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top