ads

Breaking News

32. Location Based Services in android part-1

Location Based Services in android part-1


Introduction of location-based service

⬜One of the unique features of mobile applications is location awareness. Mobile users take their devices with them everywhere, and adding location awareness to our app offers users a more contextual experience.


⬜For example, we can offer a location-based search for a pizza store, cafe, cinema -- and the phone uses the location of the phone as an input to the search.


⬜This is called location-based service.



How Android Supports LBS?

⬜To add location awareness to our apps, Android provides us 2 approaches:

                ◼️The Android Location API

                ◼️The Google Play Services Location API

⬜Initially, we will discuss the Android Location API and then in later lectures, we will cover Google Play Services Location API



The Android Location API

⬜The Android Location API contains various elements to find device’s current location.

⬜It’s two main LBS elements are:

              ◼️Location Manager

              ◼️Location Providers




The Location Manager

⬜Location Manager provides an interface to the Location-Based Services, enabling applications to obtain periodic updates of the device’s geographical location

⬜Using the Location Manager, we can:

           ◼️Obtain our current location

           ◼️Track movement

           ◼️Find available Location Providers


The Location Providers


⬜They represent various location-finding technology used to determine the device’s current location.

⬜The two popular Location Providers are GPS (Global Positioning System) and Android's Network Location Provider.

⬜The former uses satellites and the latter uses cellular technology to determine device location.


⬜GPS is exact and more appropriate for outdoors, but it consumes a lot of battery power.


⬜On the other hand, network provider works for indoors as well for outdoors and has a lower battery consumption and a faster response.


Steps Required For LBS


⬜Following are the reqd steps needed for an LBS app:

⬜Retrieve an instance of the LocationManager using a call to the getSystemService() method using the LOCATION_SERVICE.

⬜Obtain the Location object by calling the method getLastKnownLocation() of LocationManager.

⬜Call appropriate getter methods of Location object to get the location.

⬜Finally, add the required permissions in the manifest as well as the code



Step 1- Obtaining LocationManager Object

⬜The first step in is obtained a LocationManager object and for this, we call method getSystemService() of Context class.

⬜It’s prototype is :

public Object getSystemService(String serviceName)

Sample Code:

LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);


Step 2-Obtaining Location Object

⬜After getting a LocationManager object we can find the last location fix determined by a particular Location Provider using the method getLastKnownLocation( ) of LocationManager.

⬜This method accepts a provider as an argument and has the following prototype :

                    public Location getLastKnownLocation( String provider)

Sample Code:

Location lc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);


Step 3- Obtaining Location Data

⬜The Location object has got 2 methods which can tell the s exact location of the device.

These are

public double  getLatitude( )

public double  getLongitude( )



Step 4- Adding Permissions

⬜Depending on the Location Provider being used, we need to add certain permissions to our application.

⬜A GPS provider requires fine permission, while the Network provider requires only coarse.

⬜If we use fine permission, then coarse permission is implicitly added.


⬜Also , if our app targets Android 5.0 (API level 21) or higher, we must declare that our app uses the android.hardware.location.network or android.hardware.location.gps hardware feature in the manifest file, depending on whether your app receives location updates from NETWORK_PROVIDER or from GPS_PROVIDER.



⬜The permission tags that need to be added to AndroidManifest.xml for fine and coarse permissions are as follows:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<!-- Needed only if our app targets Android 5.0 (API level 21) or higher. -->    
<uses-feature android:name="android.hardware.location.gps" />
<uses-feature android:name="android.hardware.location.network" />





Thanks...


⬜◻️◻️



No comments