1 of 40

UNIT 4Enhancing User Experience

2 of 40

In this unit we will discuss the use of action bars, dialog boxes, drawables , menus and animations. Moreover we will also focus on using location based services and map services.

3 of 40

Location based services & maps

Milan V. Doshi

4 of 40

  • Introduction
  • Using the Location Based Services
  • Selecting a Location Provider
  • Finding current location
  • Creating map based activities

T

O

P

I

C

S

5 of 40

Location Based Services (LBS)

6 of 40

Introduction

Location Based Services (LBS) enable us to find the device’s current location. It includes technologies such as GPS and cell or Wi-Fi based location sensing techniques.

We can specify which technology to use explicitly by name or we can provide a set of criteria on which android select the most appropriate.

7 of 40

Using Location Based Services

LBS is a term that describes the different technologies we can use to find a device’s current location. The two main LBS elements are

  • Location Manager, provides hooks to the location-based services
  • Location Providers, each of these represents a different location

finding technology used to determine the device’s current location

8 of 40

Location Manager

Using Location Manager we can

  • Obtain the current location
  • Follow movement
  • Set proximity alerts for detecting movement into and out of

a specified area

  • Find available location providers
  • Monitor the status of the GPS receiver

9 of 40

Accessing the Location Manager

Location Manager provides access to the location based services, to access location manager we can use the getSystemService() method as

String serviceString = Context.LOCATION_SERVICE;

LocationManager LM;

LM = (LocationManager)getSystemService(serviceString);

10 of 40

Permissions to use LBS

To use location based services, we need to add permissions to our application manifest. This permissions are

ACCESS_FINE_LOCATION , represents high accuracy

ACCESS_COARSE_LOCATION , represents low accuracy

An application that has granted fine permission will have coarse permission implicitly.

11 of 40

Configuring/Using Emulators

Actually LBS are dependent upon the device hardware to find the current location. When we are developing for the emulator , our hardware is virtualized and likely to stay in the same location.

To compensate Android includes hooks that enable us to emulate Location Providers for testing LB applications.

12 of 40

Continue…

While developing in Android studio or eclipse, we can set the virtual location of the device so that we can test application accordingly.

13 of 40

Selecting a Location Provider

We can use several techniques to determine the current location, each of this technology is available as a Location Provider offers different capabilities including differences in power consumption, accuracy and the ability to determine altitude, speed, etc.

14 of 40

Finding Location Providers

The LocationManager class includes static constants that return the provider name for three Location Providers

  • LocationManager.GPS_PROVIDER
  • LocationManager.NETWORK_PROVIDER
  • LocationManager.PASSIVE_PROVIDER

We can get the list of all available providers

List<String> providers = LM.getProviders(true);

15 of 40

Specifying criteria

While finding the location , we can also mention the requirements and let android determine the best technology to use.

We can use the Criteria class for this purpose.

16 of 40

Accuracy Measures

While mentioning the criteria for location, the following measurements are used with criteria

Horizontal accuracy, High (100 m) and Low (500 m)

Vertical accuracy, High (100 m) and Low (500 m)

Bearing and Speed ,High and Low

Once we defined the Criteria , we can retrieve the best matching provider as

String bestProvider = LM.getBestProvider(criteria, true);

17 of 40

When we call the getBestProvider() , it returns the best matching location provider. If there are more than one matching providers, the method returns the one with highest accuracy.

If no provider match the criteria, the criteria is loosened in the order

  • Power use
  • Accuracy of returned location
  • Accuracy of bearing , speed and altitude
  • Availability of bearing , speed and altitude

Selection of provider by Android

18 of 40

The criteria with monetary cost is never implicitly relaxed. If no provider is found, it returns null.

We can get a list of all the providers matching our criteria

List<String> mP = LM.getProviders(criteria , false);

Continue…

19 of 40

Determining location provider capabilities

We can determine the capabilities of a location provider by retrieving it and calling the methods of the LocationProvider class.

String providerName = LocationManager.GPS_PROVIDER;

LocationProvider gpsP = LM.getProvider(providerName);

We can use the getAccuracy() and getPowerRequirement() methods to retrieve the capabilities of the selected provider.

20 of 40

Finding our current location

The powerful use of LBS is to find the current physical location of the device. The accuracy of the returned location depends on the hardware and available and the permissions requested by our application.

21 of 40

Location Privacy

Location based information is users personal information and we should respect it by

  • Using and updating location only when necessary
  • Notifying user when application track his location
  • Notify him how this information is used
  • Allow him to disable location updates

22 of 40

Finding the last known location

We can find the last location obtained by a particular Location Provider using the getLastKnownLocation() method, passing the name of the location provider.

String providerName = LocationManager.GPS_PROVIDER;

Location location = LM.getLastKnownLocation(provider);

23 of 40

Continue…

The Location object returned includes all the position information available from the provider that supplied it. It includes the time at which it was obtained, accuracy of the location found , its latitude, longitude , etc.

All of above properties are available using the getter methods.

24 of 40

Refreshing the current location

Generally the last known location is not sufficient for our application. Most location sensitive applications need to reactive to user movement. Querying the location manager for the last known location does not force it to update.

We can use the LoctionListener to receive updates in location. The requestLocationUpdates() method is used to register this listener.

25 of 40

Continue…

To optimize efficiency and minimize cost and power ,we can also specify the minimum time and the minimum distance between location change updates.

26 of 40

NJSMTI

Continue…

When the minimum time and distance values are exceeded the attached LocationListener executes its onLocationChanged() event.

27 of 40

Map Based Activities

28 of 40

Introduction

One of the best way to provide context for a physical location or address is to use a map. Using MapView , we can create activities that include an interactive map.

Using MapView ,it supports annotating using overlays and full programmatic control of the map display by including the option to display satellite view.

29 of 40

Android Map classes

  • MapView, a UI element to display the map.
  • MapActivity, to create an activity we need to extend the base class MapActivity. It handles the application life cycle and background service management required for displaying maps. We can use MapView only within these classes.
  • Overlay, it is used to annotate our maps.
  • MapController, used to control the map.

30 of 40

Continue…

  • MyLocationOverlay, used to display current position and orientation of the device.
  • ItemizedOverlays and OverlayItems, Used together to let us create a layer of map markers.

31 of 40

Map API Key

To use a MapView within our application we must obtain an API key from the Android developers website. Without an API key the MapView cannot download the titles used to display the map.

32 of 40

Creating a Map based activity

To create a map based activity, we need to extend MapActivity. The layout of this class must include a MapView to display a Google Maps interface element.

The Android maps library is not a standard android package, it must be explicitly included as an optional API in the application manifest.

<uses-library android:name=“com.google.android.maps”/>

33 of 40

Continue…

The MapView downloads its map titles on demand, as a result any application that features a MapView needs to include a uses-permission for internet access.

<uses-permission android:name=“android.permission.INTERNET”/>

34 of 40

Configuring and using Maps

By default the Map views shows the standard street map. In addition we can choose to display a satellite view and the expected traffic overlay

mapView.setSatellite(true);

mapView.setTraffic(true);

We can query for the current and maximum zoom

int maxZoom = mapView.getMaxZoomLevel();

int currZoom = mapView.getZoomLevel();

35 of 40

Continue…

We can obtain the center point and currently visible longitude and latitude span.

GeoPoint center = mapView.getMapCenter();

int latspan = mapView.getLatitudeSpan();

int longspan = mapView.getLongitudeSpan();

We can choose to display the standard map zoom controls

mapView.setBuiltInZoomControls(true);

36 of 40

Using the MapController

We can use the MapController to pan and zoom a MapView. We can use the getController() method to obtain a reference to MapController

MapController mapC = mapView.getController();

Map locations in android are represented by GeoPoint object, which contains a latitude and longitude measures in microdegrees.(1 degree = 1,000,000 microdegrees)

37 of 40

NJSMTI

Continue…

Double lat = 37.422006*1E6;

Doublr lng = -122.084095*1E6;

GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());

mapController.setCenter(point);

mapController.setZoom(1);

In setZoom() , 1 represents the widest zoom and 21 the tightest zoom. In addition, setCenter() jumps to a new location, to show a smooth transition we can use animateTo().

mapController.animateTo(point);

38 of 40

Creating and using overlays

Overlays enable us to add annotations and click handling to MapView. Each overlay enables us to draw 2D primitives, including text, lines, images and shapes.

39 of 40

My Location overlay

The MyLocationOverlay class is a native overlay designed to show our current location and orientation.

List<Overlay> overlays = mapView.getOverlays();

MyLocationOverlay mlo = new MyLocationOverlay(this, mapView);

overlays.add(mlo);

We can display current location and orientation

mlo.enableCompass();

mlo.enableMyLocation();

40 of 40

Itemized overlays and overlay items

  • OverlayItems are used to supply simple marker functionality to our MapView via the ItemizedOverlay class.
  • ItemizedOverlay provide a convenient way for adding markers to a map, letting us to add image and text to a particular geographical position. It handles the drawing, placement, click handling, focus control and layout optimization of each OverlayItem marker.