Geting current location in Android application, using GPS
February 25, 2011 19 Comments
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


Hi, thanks for ur code
plz help me !!
but I have some Q’s
umm the 3rd code where should I write it ? in class Avtivity ?
can you give the whole source code ?
umm and I tried a lot of codes and still GPS not working in my emulator?
welcome Hiba,
that code will b inside activity class only. you will need to create the instance of the button
Button btn_show_location= (Button)findViewById(R.id.btn_show_location);
and then, write the 3rd step code on its click listener.
I hope now you can get it.
i can’t access my positon from my app give me the solution
what problem you are getting?
i get error when i write alert message plz give the solution
hello nitin, what error does it alerts?
can you please witr whole code… or upload it link ?? i have also problem as Hiba
Hi engiguide,
you can download source project from my new post
how to get current position from my app
Hello, what is the reason for placing loc.getLatitude and loc.getLongitude before the lines where their values are actually stored?
Thanks,
Noah
how to find the my Position from my android aplliacation
You may need to compare the location lat and lingt. with received latitude and longitude.
hey Prashant ,
nice tutorial but i’m getting ILLEGAL ARGUMENT EXCEPTION… can u plz enlight me where i go wrong??
thankx..
Sorry Mani, no idea about the error. but this is running example.
“else
{
alert.setTitle(“Wait”);
alert.setMessage(“GPS in progress, please wait.”);
alert.setPositiveButton(“OK”, null);
alert.show();
}
”
only this part of code is getting executed always when i launch from emulator
Can we send source code for positioning the current location using Google map in android…
Sorry Ramaniraj, never tried that.
Prashant I jst want the code which always locate our location. with a pushpin
could u sent me it.?
what about if i want it to be done automatically without intention .. for example if the phone is stolen and the thief entered wrong password , or if i wanna trace some one ‘s phone using the serial number