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

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?

    Thanks,
    Noah

  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??
    thankx..

  6. shalini says:

    “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

  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
    requestLocationUpdates()

  11. shubhangi says:

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

Leave a reply to Noah Cancel reply