how can i get my current gps location android

project Name : MyCurrentLocation
target : 2.1
menifest permission :  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

activity Name : MyCurrentLocationActivity.java


MyCurrentLocationActivity.java coding
====================================


public class MyCurrentLocationActivity extends Activity
{
Button btnGps;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btnGps=(Button)findViewById(R.id.button1);

btnGps.setOnClickListener(new OnClickListener() {
  @Override
public void onClick(View v) {
// TODO Auto-generated method stub
 
  /* Use the LocationManager class to obtain GPS locations */
  System.out.println( "Click ....");
 
  LocationManager mylocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  LocationListener mlocListener = new MyAApsLocationListener();
  mylocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
});

}


/* Class My Location Listener */

public class MyAApsLocationListener implements LocationListener
{
    public void onLocationChanged(Location loc)
      {
loc.getLatitude();
loc.getLongitude();

Toast.makeText( getApplicationContext(),"My current location is: " +" Latitud = " + loc.getLatitude() +" Longitud = " + loc.getLongitude(),Toast.LENGTH_SHORT).show();
   }


   @Override
     public void onProviderDisabled(String provider)
{
    Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
}


  @Override
     public void onProviderEnabled(String provider)
      {
       Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
      }


     public void onStatusChanged(String provider, int status, Bundle extras)
     {
     }

    }

}

//////////////////////////////////////main.xml////////////////////////////////////////



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />


    <Button
        android:id="@+id/button1"
        android:layout_width="85dp"
        android:layout_height="wrap_content"
        android:text="Get GPS" />

</LinearLayout>

/////////////////////////////////////////// Menifest.xml ///////////////////////////////////////////////////



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myandroid.location"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MyCurrentLocationActivity"
            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>
   "

</manifest>

///////////////////////////////////////////////////////////////////////////////

if you run this application on real device then fine :
but if you run  AVD then you need to provide lat long by
 Emulator Control ,which you can find by this path
eclipse->window->show View->Emulator Control
then ,
           






















No comments:

IRCTC Share Price Declines by 2% Despite 30% Jump in Q4 Net Profit; Board Announces Dividend of INR 2 per Share

Introduction: The share price of Indian Railway Catering and Tourism Corporation (IRCTC) experienced a decline of 2% in today's trading ...