Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Accessing GPS Information in Your Application

Author: Pratik Rupwal -- Published? true -- FormatLanguage: W

Problem:

You need access the GPS location in any class of your application.

Solution:

Add a class (say 'MyLocationListener') which implements 'LocationListener' interface. create an instance of this class where you want to access the GPS information and use it for the relevant requirement.

Discussion:

Below class 'MyLocationListener' implements 'LocationListener'

public class MyLocationListener implements LocationListener
	{
		
		@Override
		public void onLocationChanged(Location loc)
		{
			loc.getLatitude();
			loc.getLongitude();
                        /** The above 'Location' object 'loc' can be used for accessing GPS information, however it is not always possible
                        in an application to perform all the GPS information related tasks in this overridden method due to reasons like 
                        data accessiblity.
                                 for example in an application providing the information of shopping malls near the user's current location,
                        so app accesses the names of malls according to the user's location and displays them to user,on choosing one of them
                        app displays the different offers given by that mall.
                                 In this example the location of the user decides the mall's name to be fetched from database through a
                        database handler which is private member of the class hosting the view to display list of mall,hence that database
                        handler can't be accessible in this overridden method hence this operation cannot be carried out.*/
		}
		
		@Override
		public void onProviderDisabled(String provider)
		{
			
		}
		@Override
		public void onProviderEnabled(String provider)
		{
			
		}
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras)
		{
			
		}
	}// End of Class MyLocationListener.

Add the above class file in package of your application, its instance can be used as below for accessing GPS information in any class.


public class AccessGPS extends Activity
{
//declaration of required objects

LocationManager mlocManager;
LocationListener mlocListener;
Location lastKnownLocation;
Double latitude,longitude;
......
......

protected void onCreate(Bundle savedInstanceState)
{
   ........
   ........
//instantiating objects for accessing GPS information.

mlocListener = new MyLocationListener();

//reuest for location updates

mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
locationProvider=LocationManager.GPS_PROVIDER;
.......
.......

// Access the last identified location

lastKnownLocation = mlocManager.getLastKnownLocation(locationProvider);

// The above object can be used for accessing GPS data as below 

latitude=lastKnownLocation.getLatitude();
longitude=lastKnownLocation.getLongitude();

// The above GPS data can be used for carrying out the operations specific to the location. 
.......
.......

}
}
No records found.