UNIT 4�Enhancing User Experience
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.
Location based services & maps
Milan V. Doshi
T
O
P
I
C
S
Location Based Services (LBS)
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.
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
finding technology used to determine the device’s current location
Location Manager
Using Location Manager we can
a specified area
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);
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.
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.
Continue…
While developing in Android studio or eclipse, we can set the virtual location of the device so that we can test application accordingly.
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.
Finding Location Providers
The LocationManager class includes static constants that return the provider name for three Location Providers
We can get the list of all available providers
List<String> providers = LM.getProviders(true);
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.
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);
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
Selection of provider by Android
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…
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.
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.
Location Privacy
Location based information is users personal information and we should respect it by
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);
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.
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.
Continue…
To optimize efficiency and minimize cost and power ,we can also specify the minimum time and the minimum distance between location change updates.
NJSMTI
Continue…
When the minimum time and distance values are exceeded the attached LocationListener executes its onLocationChanged() event.
Map Based Activities
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.
Android Map classes
Continue…
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.
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”/>
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”/>
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();
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);
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)
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);
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.
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();
Itemized overlays and overlay items