Advertisement
ScottHelme

GPS Tracker - LocationServices

Jul 18th, 2013
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.47 KB | None | 0 0
  1. package com.prodev.litetrack;
  2.  
  3. import android.content.Context;
  4. import android.location.Location;
  5. import android.location.LocationListener;
  6. import android.location.LocationManager;
  7. import android.os.Bundle;
  8.  
  9. /***
  10.  * Location Helper Class.
  11.  * Handles creation of the Location Manager and Location Listener.
  12.  *
  13.  * @author Scott Helme
  14.  */
  15. public class LocationServices{
  16.        
  17.         //variables to store lat and long
  18.         private float latitude  = 0.0f;
  19.         private float longitude = 0.0f;
  20.        
  21.         //flag for when we have co-ords
  22.         private boolean gotLocation = false;
  23.        
  24.         //my location manager and listener
  25.         private LocationManager    locationManager;
  26.         private MyLocationListener locationListener;
  27.        
  28.         /**
  29.          * Constructor.
  30.          *
  31.          * @param context - The context of the calling activity.
  32.          */
  33.     public LocationServices(Context context){
  34.  
  35.         //setup the location manager
  36.         locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
  37.         //create the location listener
  38.         locationListener = new MyLocationListener();
  39.        
  40.       //setup a callback for when the GRPS/WiFi gets a lock and we receive data
  41.         locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
  42.         //setup a callback for when the GPS gets a lock and we receive data
  43.         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
  44.     }
  45.        
  46.     /***
  47.      * Used to receive notifications from the Location Manager when they are sent. These methods are called when
  48.      * the Location Manager is registered with the Location Service and a state changes.
  49.      *
  50.      * @author Scott Helme
  51.      */
  52.      public class MyLocationListener implements LocationListener {
  53.            
  54.         //called when the location service reports a change in location
  55.         public void onLocationChanged(Location location) {
  56.                
  57.         //store lat and long
  58.         latitude = (float) location.getLatitude();
  59.         longitude = (float) location.getLongitude();
  60.  
  61.         //now we have our location we can stop the service from sending updates
  62.         //comment out this line if you want the service to continue updating the users location
  63.         //locationManager.removeUpdates(locationListener);
  64.        
  65.         //change the flag to indicate we now have a location
  66.         gotLocation = true;
  67.        
  68.         }
  69.  
  70.                 //called when the provider is disabled
  71.         public void onProviderDisabled(String provider) {}
  72.         //called when the provider is enabled
  73.         public void onProviderEnabled(String provider) {}
  74.         //called when the provider changes state
  75.         public void onStatusChanged(String provider, int status, Bundle extras) {}
  76.         }
  77.        
  78.         /***
  79.          * Stop updates from the Location Service.
  80.          */
  81.         public void killLocationServices(){
  82.                 locationManager.removeUpdates(locationListener);
  83.         }
  84.        
  85.         /***
  86.          * Start updates from the Location Service.
  87.          */
  88.         public void startLocationServices(){
  89.             //setup a callback for when the GRPS/WiFi gets a lock and we receive data
  90.             locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
  91.             //setup a callback for when the GPS gets a lock and we receive data
  92.             locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
  93.         }
  94.        
  95.         /***
  96.          * Get Latitude from GPS Helper.
  97.          * Should check gotLocation() first.
  98.          * @return - The current Latitude.
  99.          */
  100.         public float getLat(){
  101.                 return latitude;
  102.         }
  103.        
  104.         /***
  105.          * Get Longitude from GPS Helper.
  106.          * Should check gotLocation() first.
  107.          * @return - The current Longitude.
  108.          */
  109.         public float getLong(){
  110.                 return longitude;
  111.         }
  112.        
  113.         /***
  114.          * Check if a location has been found yet.
  115.          * @return - True if a location has been acquired. False otherwise.
  116.          */
  117.         public Boolean gotLocation(){
  118.                 return gotLocation;
  119.         }
  120.        
  121.         /***
  122.          * Reset location flag to get a new one next time service is used.
  123.          */
  124.         public void resetLocation(){
  125.             gotLocation = false;
  126.         }  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement