Advertisement
sgccarey

Android location app

Oct 11th, 2011
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. /* V0.01
  2.  * 4/10/11 - 2010
  3.  * Just network data location
  4.  *
  5.  */
  6.  
  7. package com.saltapps.satmap;
  8.  
  9. import android.app.Activity;
  10. import android.location.Criteria;
  11. import android.location.LocationListener;
  12. import android.location.LocationManager;
  13. import android.os.Bundle;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. public class Location extends Activity implements LocationListener {    // implement LocationListener, for getting network location data
  18.  
  19.       LocationManager locationManager;
  20.        
  21.       TextView tvLat, tvLongi;                      // textviews for location
  22.       String towers;    // used with location manager, to get location from service provider masts
  23.  
  24.       float lat, longi;         // values for Location (longi, since long is Java keyword)
  25.      
  26.      
  27.       @Override
  28.       public void onCreate(Bundle savedInstanceState) {
  29.         super.onCreate(savedInstanceState);
  30.         setContentView(R.layout.location);  // lays the activity out according to the main.xml file in the res/layout folder
  31.        
  32.           locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);   //  get location manager
  33.  
  34.         tvLat = (TextView) findViewById(R.id.tvLat);            // for displaying latitude and longitude values
  35.         tvLongi = (TextView) findViewById(R.id.tvLongi);
  36.        
  37.         Criteria crit = new Criteria();    
  38.         towers = locationManager.getBestProvider(crit, false);  // gets best data according to criteria. Our criteria is default, so we should get best data
  39.         android.location.Location location = locationManager.getLastKnownLocation(towers);  // gets last known location using network data from masts
  40.        
  41.         if (location != null) {                     // checks that location information is present
  42.             lat = (float) (location.getLatitude());     // gets latidude
  43.             longi = (float) (location.getLongitude());  // gets longitude
  44.            
  45.             tvLat.setText("Latitude: " + lat);          // displays latitude
  46.             tvLongi.setText("Longitude: " + longi);     // displays longitude
  47.            
  48.         } else {
  49.             Toast.makeText(this, "Couldn't get provider", Toast.LENGTH_SHORT).show();   // displays briefly
  50.         }   // if/else
  51.              
  52.       } // onCreate
  53.  
  54.  
  55.       @Override
  56.       public void onResume() {  // actions to carry out upon returning to this activity
  57.         super.onResume();       //
  58.       } // onResume
  59.  
  60.  
  61.       @Override
  62.       public void onPause() {   // actions to carry out when activity isn't in focus
  63.         super.onPause();
  64.       } // onPause
  65.  
  66.      
  67.  
  68.     public void onLocationChanged(Location location) {    // Ignore (for now)
  69.         // TODO Auto-generated method stub 
  70.     }   // onLocationChanged
  71.  
  72.  
  73.     public void onProviderDisabled(String provider) {     // Ignore (for now)
  74.         Toast.makeText(this, "Couldn't get provider", Toast.LENGTH_SHORT).show();
  75.     }   // onProviderDisabled
  76.  
  77.  
  78.     public void onProviderEnabled(String provider) {      // Ignore (for now)
  79.         // TODO Auto-generated method stub
  80.     }   // onProviderEnabled
  81.  
  82.  
  83.     public void onStatusChanged(String provider, int status, Bundle extras) {     // Ignore (for now)
  84.         // TODO Auto-generated method stub
  85.     }   // onStatusChanged
  86.  
  87.  
  88.     public void onLocationChanged(android.location.Location arg0) {
  89.         // TODO Auto-generated method stub
  90.        
  91.     }   // onLocationChanged
  92.  
  93.  
  94. }   // class
  95.  
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement