Geting current location in Android application, using GPS

Today one another functionality I tried in the android application. It was to provide the longitude and latitude of the current location in android using it GPS system. The coding was quite easy.  We have to create a LocationManager and LocationListener. And sending the GPS_PROVIDE, we can get the current location value in term of longitude and latitude.

Here are the codes I have implemented in my project

1.       Enable the ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION by adding the following two tag (line 18,19) into the Minifest xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.yms"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name" >
        <activity android:name=".Login"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        .
        .
        .
    </application>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
</manifest>

2.       Write a class which will return the longitude and latitude using the current GPS Location

package com.testgps;

import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;

public class MyLocationListener implements LocationListener {

    public static double latitude;
    public static double longitude;

    @Override
	public void onLocationChanged(Location loc)
	{
		loc.getLatitude();
		loc.getLongitude();
		latitude=loc.getLatitude();
		longitude=loc.getLongitude();
	}

	@Override
	public void onProviderDisabled(String provider)
	{
		//print "Currently GPS is Disabled";
	}
	@Override
	public void onProviderEnabled(String provider)
	{
		//print "GPS got Enabled";
	}
	@Override
	public void onStatusChanged(String provider, int status, Bundle extras)
	{
	}
}

3.       On button click call onLocationChange, which will return the value that could be assigned to EditText or any other control

btn_show_location.setOnClickListener(new View.OnClickListener() {
     public void onClick(View view) {
         LocationManager mlocManager=null;
         LocationListener mlocListener;
         mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
         mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

        if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            if(MyLocationListener.latitude>0)
            {
                 et_field_name.append("Latitude:- " + MyLocationListener.latitude + '\n');
                 et_field_name.append("Longitude:- " + MyLocationListener.longitude + '\n');
             }
             else
             {
                  alert.setTitle("Wait");
                  alert.setMessage("GPS in progress, please wait.");
                  alert.setPositiveButton("OK", null);
                  alert.show();
              }
          } else {
              et_field_name.setText("GPS is not turned on...");
          }

      }
 });

Referred from firstdroid.com