Location Based Services (Android) - AAD - Unit III

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

Location Based Services

(Android)
SAM DHANA SEKAR S P
DEPARTMENT OF CS, RCAS
Introduction

 One of the unique features of mobile applications is location awareness.


 Mobile users take their devices with them everywhere, and adding location awareness to your app
offers users a more contextual experience.
 Using the Google Play services location APIs, your app can request the last known location of the
user's device
 user's current location - usually equivalent to the last known location of the device.

Location API in Android

 android.location
 Contains the framework API classes that define Android location-based and related services.
 Interfaces
 LocationListener - Used for receiving notifications from the LocationManager when the location has
changed.
 Classes
 Location - A data class representing a geographic location.
 LocationManager - This class provides access to the system location services.
 LocationProvider - An abstract superclass for location providers.
Location Provider

 Location Provider
 A location provider provides periodic reports on the geographical location of the device.
 Each provider has a set of criteria under which it may be used;
 GPS_PROVIDER - require GPS hardware and visibility to a number of satellites;
 This provider determines location using satellites.
 Depending on conditions, this provider may take a while to return a location fix.
 Requires the permission Manifest.permission.ACCESS_FINE_LOCATION.

 NETWORK_PROVIDER
 require the use of the cellular radio, or access to a specific carrier's network, or to the internet.
 Requires the permission Manifest.permission.ACCESS_COARSE_LOCATION.
Location - Class

 A data class representing a geographic location.


 A location can consist of
 a latitude,
 longitude,
 timestamp, and
 other information such as bearing, altitude and velocity.
Location Class - methods

 static void distanceBetween(double startLatitude, double startLongitude, double endLatitude,


double endLongitude, float[] results)
 Computes the approximate distance in meters between two locations, and optionally the initial and final
bearings of the shortest path between them.

 float distanceTo(Location dest)


 Returns the approximate distance in meters between this location and the given location.
Location - Class

 double getAltitude()
 Get the altitude if available.

 double getLatitude()
 Get the latitude, in degrees.

 double getLongitude()
 Get the longitude, in degrees.

 String getProvider()
 Returns the name of the provider that generated this fix.
LocationListener - interface

 Used for receiving notifications from the LocationManager when the location has changed.
 Following methods are called if the LocationListener has been registered with the location manager
service using the
LocationManager.requestLocationUpdates(String, long, float, LocationListener) method.

 abstract void onLocationChanged(Location location) - Called when the location has changed.
 abstract void onProviderDisabled(String provider) - Called when the provider is disabled by the user.
 abstract void onProviderEnabled(String provider) - Called when the provider is enabled by the user.
 abstract void onStatusChanged(String provider, int status, Bundle extras)
LocationManager

 This class provides access to the system location services. These services allow applications to
obtain periodic updates of the device's geographical location
 methods
 boolean isLocationEnabled()
 Returns the current enabled/disabled state of location.

 boolean isProviderEnabled(String provider)


 Returns the current enabled/disabled status of the given provider.
 Boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
LocationManager - methods

 Location getLastKnownLocation(String provider)


 Returns a Location indicating the data from the last known location fix obtained from the given provider.
 if (locationManager != null)
{
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
LocationManager - methods

 LocationProvider getProvider(String name)


 Returns the information associated with the location provider of the given name, or null if no provider exists by
that name.

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


 Register for location updates using the named provider.
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds

private static final long MIN_TIME_BW_UPDATES = 200 * 10 * 1; // 2 seconds


locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Note1: Set up Google Play services

Set up Google Play services


In SDK MANAGER
Note 2: Specify app permissions

 Apps that use location services must request location permissions


1. android.permission.ACCESS_COARSE_LOCATION
2. android.permission.ACCESS_FINE_LOCATION
3. android.permission.ACCESS_BACKGROUND_LOCATION
 1 & 2  The permission you choose determines the accuracy of the
location returned by the API.
 3  need to access the device location while your app is in the
background
Specify the permission in Mainfest File

You might also like