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=""
    <application android:icon="@drawable/icon" android:label="@string/app_name" >
        <activity android:name=".Login"
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

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;

	public void onLocationChanged(Location loc)

	public void onProviderDisabled(String provider)
		//print "Currently GPS is Disabled";
	public void onProviderEnabled(String provider)
		//print "GPS got Enabled";
	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)) {
                 et_field_name.append("Latitude:- " + MyLocationListener.latitude + '\n');
                 et_field_name.append("Longitude:- " + MyLocationListener.longitude + '\n');
                  alert.setMessage("GPS in progress, please wait.");
                  alert.setPositiveButton("OK", null);
          } else {
              et_field_name.setText("GPS is not turned on...");


Referred from

23 Responses to Geting current location in Android application, using GPS

  1. Hiba says:

    Hi, thanks for ur code
    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? 😐 plz help me !!

  2. engiguide says:

    can you please witr whole code… or upload it link ?? i have also problem as Hiba

  3. Noah says:

    Hello, what is the reason for placing loc.getLatitude and loc.getLongitude before the lines where their values are actually stored?


  4. nitin says:

    how to find the my Position from my android aplliacation

  5. mani says:

    hey Prashant ,
    nice tutorial but i’m getting ILLEGAL ARGUMENT EXCEPTION… can u plz enlight me where i go wrong??

  6. shalini says:

    alert.setMessage(“GPS in progress, please wait.”);
    alert.setPositiveButton(“OK”, null);;

    only this part of code is getting executed always when i launch from emulator

  7. Ramaniraj S says:

    Can we send source code for positioning the current location using Google map in android…

  8. umesh says:

    Prashant I jst want the code which always locate our location. with a pushpin
    could u sent me it.?

  9. 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

  10. Alex says:

    i got error like this, java.lang.RuntimeException: Can’t create handler inside threat that has not calles Looper.prepare(), what does it means?, and what to do to fix this?, i got error when calling

  11. shubhangi says:

    i dont get the current location on android phone…
    Please help me…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: