Don't like ads? PRO users don't see any ads ;-)
Guest

Android GPS

By: a guest on May 5th, 2012  |  syntax: Java  |  size: 1.95 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class GPSLocator implements LocationListener{
  2.        
  3.  
  4.         LocationManager locMgr;
  5.        
  6.         private double latitude = 0, longitude = 0;
  7.         private double accuracy;
  8.        
  9.     /** Called when the activity is first created. */
  10.     @Override
  11.     public void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.        
  14.                 ...
  15.        
  16.         locMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  17.         locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
  18.         LocationProvider p = locMgr.getProvider(LocationManager.GPS_PROVIDER);
  19.         Toast.makeText(this, "Requires Satellite: " + p.requiresSatellite(), Toast.LENGTH_LONG).show();
  20.     }
  21.    
  22.     public void onDestroy(){
  23.         locMgr.removeUpdates(this);
  24.         super.onDestroy();
  25.     }
  26.    
  27.         /**
  28.         * The following are methods of the LocationListener class
  29.         */
  30.         @Override
  31.         public void onLocationChanged(Location loc) {
  32.                 latitude = loc.getLatitude();
  33.                 longitude = loc.getLongitude();
  34.                 accuracy = loc.getAccuracy();
  35.         }
  36.        
  37.         @Override
  38.         public void onStatusChanged(String provider, int status, Bundle extras) {
  39.                 switch (status) {
  40.                 case LocationProvider.OUT_OF_SERVICE:
  41.                         Toast.makeText(getApplicationContext(), "Status Changed: Out of Service",
  42.                                         Toast.LENGTH_SHORT).show();
  43.                         break;
  44.                 case LocationProvider.TEMPORARILY_UNAVAILABLE:
  45.                         Toast.makeText(getApplicationContext(), "Status Changed: Temporarily Unavailable",
  46.                                         Toast.LENGTH_SHORT).show();
  47.                         break;
  48.                 case LocationProvider.AVAILABLE:
  49.                         Toast.makeText(getApplicationContext(), "Status Changed: Available",
  50.                                         Toast.LENGTH_SHORT).show();
  51.                         break;
  52.                 }
  53.         }
  54.        
  55.         @Override
  56.         public void onProviderEnabled(String provider) {
  57.                 Toast.makeText(getApplicationContext(), "GPS Enabled", Toast.LENGTH_SHORT).show();
  58.         }
  59.        
  60.         @Override
  61.         public void onProviderDisabled(String provider) {
  62.                 Toast.makeText(getApplicationContext(), "GPS Disabled", Toast.LENGTH_SHORT).show();
  63.         }
  64. };