Breaking News

33. Location Based Services in android Part - 2

Location Based Services in android Part - 2

Tracking The Device

◻️ Most location-sensitive applications will need to be reactive to user movement i.e. as soon as the user moves a particular distance the location should be obtained.

◻️ To do this in Android we use the method
requestLocationUpdates()  of LocationManager class .

◻️ The method has the following prototype:

public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener) 


provider:the name of the provider with which to register

minTime :the minimum time interval for notifications, in milliseconds.

minDistance : the minimum distance interval for notifications, in meters

listener :a  LocationListener object whose onLocationChanged(Location) method will be called for each location update

◻️ Sample Call

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 36000, 1000,obj);

◻️This example invokes the method
    onLocationChanged( ) whenever either of the two events occur:

1.After 36000 milliseconds OR
2.When the distance travelled by the device is 1000  meters

LocationListener Methods

Four methods may be invoked by the LocationListener, depending on the occurrence of different events. The methods are

• public void onLocationChanged(Location location)—Called when the location of the device is changed or when the minimum time and distance values are exceeded

• public void onProviderDisabled(String provider)—Called when the location provider is disabled by the user

• public void onProviderEnabled(String provider)—Called when the location provider is enabled by the user

• public void onStatusChanged(String provider, int status, Bundle extras)—Called when the provider status changes from available to unavailable or vice versa

Modified getLocation() Method

Now we modify the getLocation( ) method we defined in the previous app so that we can get periodic updates regarding the device location.

We mainly have to do 3 changes:

Create an inner class as implementation class of OnLocationChangedListener and override the method onLocationChanged()

Create an object of this implementation class

Register this object as listener by calling the method requestLocationUpdates( )

public void getMyLocation(View view) {
 LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    MyLocationListener obj=new MyLocationListener();
 lm.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 5000, 1000, obj);

public class MyLocationListener implements LocationListener {
 public void onLocationChanged(Location loc) {
 String text="";
 text = "My current location is:\n" +"Latitude = " + loc.getLatitude() +"\nLongitude = " + loc.getLongitude();
 tv.setText( text);

public void onProviderDisabled(String provider) {

public void onProviderEnabled(String provider) {

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

Obtaining Street Addresses

◻️Determining the latitude and longitude is useful for precise location, tracking, and measurements; however, it’s not usually descriptive to users. 

◻️The Android SDK provides some helper methods to turn raw location data into addresses and vice-versa.

◻️The process of translating between street addresses and longitude/latitude GPS locations is known as geocoding. 

◻️There are two types of geocoding:

       ◽Forward Geocoding—Translates street addresses into latitude and longitude values

      ◽Reverse Geocoding—Translates latitude and longitude values into the street addresses

◻️Both are done using a class called Geocoder

Steps required for reverse Geo Coding

⬜To perform reverse geocoding , we need following steps:

          ◽Create an object of Geocoder class

          ◽Call it’s method getFromLocation( ) which returns a list of Address objects corresponding to the location coordinates passed.

         ◽Finally call the methods of Address object to get the street address, postal code, country name etc

Step -1 Create GeoCoder Object

◻️To create a Geocoder object we have to call the following constructor:

public Geocoder(Context,Locale)


1.Reference to Context
2.The desired Locale for the query results


Geocoder geoobj = new Geocoder(this, Locale.ENGLISH);

Step - 2 Calling getFromLocation()

◻️Once we have got the object of Geocoder , the next step is to obtain the street address corresponding to the latitude/longitude we have.

◻️To do this we call the getFromLocation( ) method of Geocoder. It has the following prototype:
List<Address> getFromLocation(double latitude, double longitude, int maxResults) throws IOException


List<Address> addr = geoobj.getFromLocation(lat,lng, 1);

Getting Street Address

◻️This will return a list of all the addresses pertaining to the given latitude and longitude.

◻️This list is a collection of Address objects where each object contains detailed address like locality, postal code, country name etc

◻️To get these details we can call varoius methods of Address class

Important method of Address Classess

Important method of Address Classess



No comments