Just Simple Info

Pages

Android get Latitude, Longitude and Location Accuracy using Gps and Network.

Hello Friends, every android phone has ability to get your current location via Gps or Network.
This post is simple yet effective to get your current location using Gps and Network.

Gps provide the most accurate coordinates not like network but sometimes network does.
By the way, android also give as  how possible far we are in given position.
It's called the Accuracy. The smallest number of accuracy the most accurate location.

Here is the code snippet:


public class MainActivity extends Activity implements OnClickListener {

   RelativeLayout contentView;
   LinearLayout linearLayout;
   TextView longitudeTextView;
   TextView latitudeTextView;
   TextView accuracyTextView;
   TextView speedTextView;
   TextView locationProviderTextView;
   TextView providerStatusTextView;
 
 
   Button getLocationBtn;
   Button stopBtn;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        this.initComponents();
    }

    final static String LONGITUDE_LBL = "Longitude";
    final static String LATITUDE_LBL = "Latitude";
    final static String ACCURACY_LBL = "Accuracy";
    final static String SPEED_LBL = "Speed";
    final static String FIND_LOCATION_BTN_LBL = "Find Location";
    final static String STATUS_LBL = "Status";
    final static String STOP_LBL = "Stop Finding Location";
    final static String PROVIDER_STATUS_LBL = "Provider Status";
    final static String LOCATION_PROVIDER_LBL = "Location Provider";
    
    private void initComponents(){
     this.contentView = new RelativeLayout(this);
     
     this.linearLayout = new LinearLayout(this);
     this.linearLayout.setOrientation(LinearLayout.VERTICAL);
     
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
     params.addRule(RelativeLayout.CENTER_IN_PARENT);
     
     this.linearLayout.setLayoutParams(params);
    
     this.contentView.addView(this.linearLayout);
     
     this.longitudeTextView = new TextView(this);
     this.longitudeTextView.setText(LONGITUDE_LBL+" : ");
     
     this.latitudeTextView = new TextView(this);
     this.latitudeTextView.setText(LATITUDE_LBL+" :");
     
     this.accuracyTextView = new TextView(this);
     this.accuracyTextView.setText(ACCURACY_LBL +" :");
     
     this.speedTextView = new TextView(this);
     this.speedTextView.setText(SPEED_LBL +" :");
     
     this.providerStatusTextView = new TextView(this);
     this.providerStatusTextView.setText(PROVIDER_STATUS_LBL+" : ");
     
     this.locationProviderTextView = new TextView(this);
     this.locationProviderTextView.setText(LOCATION_PROVIDER_LBL+" : ");
     
     this.getLocationBtn = new Button(this);
     this.getLocationBtn.setText(FIND_LOCATION_BTN_LBL);
     
     // set click listener
     this.getLocationBtn.setOnClickListener(this);
     
     
     this.stopBtn = new Button(this);
     this.stopBtn.setText(STOP_LBL);
     
     // set click listener
     this.stopBtn.setOnClickListener(this);
     
     
     this.linearLayout.addView(locationProviderTextView);
     this.linearLayout.addView(longitudeTextView);
     this.linearLayout.addView(latitudeTextView);
     this.linearLayout.addView(accuracyTextView);
     this.linearLayout.addView(speedTextView);
     this.linearLayout.addView(providerStatusTextView);
     this.linearLayout.addView(getLocationBtn);
     this.linearLayout.addView(stopBtn);
     
     
     
     this.setContentView(contentView);
    }

     LocationManager lm;
     LocationListener gpsLocationListener;
     LocationListener networkLocationListener;
    
 @Override
 public void onClick(View view) {
  
  if(((Button)view).getText().toString().trim().equalsIgnoreCase(FIND_LOCATION_BTN_LBL)){
   // find location
   
   if(this.lm == null){
    
    this.lm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    this.gpsLocationListener = new MyGpsLocationListener();
    this.networkLocationListener = new MyGpsLocationListener();
    
    // get location via GPS satellite
    this.lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsLocationListener);
    
    // get location using network
    this.lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networkLocationListener);
    
    
    providerStatusTextView.setText(PROVIDER_STATUS_LBL+" : WAITING...");
    Toast.makeText(view.getContext(), "Starting getting yout location.", Toast.LENGTH_SHORT).show();
    
   }else{
    
    Toast.makeText(view.getContext(), "Getting location is active already.", Toast.LENGTH_SHORT).show();
    
   }
   
  }else{
   // stop finding location
   
   if(this.lm !=null){
    // stop getting location
    this.lm.removeUpdates(this.gpsLocationListener);
    this.lm.removeUpdates(networkLocationListener);
    this.lm = null;
    
    providerStatusTextView.setText(PROVIDER_STATUS_LBL+" : Stopped.");
    Toast.makeText(view.getContext(), "Getting location stopped.", Toast.LENGTH_SHORT).show();
    
   }else{
    // nothing to stop
    Toast.makeText(view.getContext(), "Location finder is not active.", Toast.LENGTH_SHORT).show();
    
   }
   
  }
  
  
  
  
  
 }

 /**
  * 
  * @author lau
  *
  */
   class MyGpsLocationListener implements LocationListener{

 @Override
 public void onLocationChanged(Location location) {
  
  if(location != null){
   
   latitudeTextView.setText(LATITUDE_LBL+" : "+ location.getLatitude());
   longitudeTextView.setText(LONGITUDE_LBL + " : " +location.getLongitude()+"");
   accuracyTextView.setText(ACCURACY_LBL +" : "+location.getAccuracy());
   speedTextView.setText(SPEED_LBL+" : "+location.getSpeed());
   locationProviderTextView.setText(LOCATION_PROVIDER_LBL+" : "+location.getProvider());
   
   providerStatusTextView.setText(PROVIDER_STATUS_LBL+" : AVAILABLE");
   
   
  }
  
 }

 @Override
 public void onStatusChanged(String provider, int status, Bundle extras) {
  // TODO Auto-generated method stub
  if(status == LocationProvider.AVAILABLE){
  
   providerStatusTextView.setText(PROVIDER_STATUS_LBL+" : AVAILABLE");
   
  }else if( status == LocationProvider.OUT_OF_SERVICE){
   
   providerStatusTextView.setText(PROVIDER_STATUS_LBL+" : OUT_OF_SERVICE");
   
   
  }else if(status == LocationProvider.TEMPORARILY_UNAVAILABLE){
   
   providerStatusTextView.setText(PROVIDER_STATUS_LBL+" : TEMPORARILY_UNAVAILABLE");
   
  }else{
   
   providerStatusTextView.setText(PROVIDER_STATUS_LBL+" : UNKOWN");
  }
  
  
 }

 @Override
 public void onProviderEnabled(String provider) {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void onProviderDisabled(String provider) {
  // TODO Auto-generated method stub
  
 }
    
   }
   
}

Add below code snippet in manifest.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Make sure the location is not disable.

Hope this code is useful.

Good luck and happy coding everyone.

Download source code here.

No comments:

Post a Comment