Using Android Networking APIs
Using Location-Based Services (LBS)
APIs Whether for safety or for convenience, location-based features on cell phones are mostly standard these days.As such, incorporating location information, navigation, and mapping features into your project can make your application much more robust. In this chapter, you learn how to leverage location-based services available within the Android SDK.You learn how to determine the location of the handset using a particular device hardware provider, such as a built-in Global Positioning Systems (GPS) unit.You also learn how to translate raw location coordinates into descriptive location names—and how to do the reverse. Finally, we explore a couple of different methods for mapping and utilities that work with the maps. Using Global Positioning Services (GPS) The Android SDK provides means for accessing location via a built-in GPS hardware, when it’s available. Generally speaking, just about every Android phone has some LBS capabilities. For example, in the United States, mobile phone location information is used by emergency services.That said, not all Android devices are phones, nor do all phones enable consumer-usage of LBS services. If GPS features are disabled, or an Android device does not have LBS hardware, the Android SDK provides additional APIs for determining alternate location providers.These other providers might have advantages and disadvantages in terms of power use, speed, and accuracy of reporting. Tip Many of the code examples provided in this chapter are taken from the SimpleLocation application. The source code for this application is provided for download on the book website. 316 Chapter 14 Using Location-Based Services (LBS) APIs
Using GPS Features in Your Applications
LBS services and hardware such as a built-in precision GPS are optional features for Android devices. In addition to requiring the appropriate permissions, you can specify which optional features your application requires within the Android Manifest file.You can declare that your application uses or requires specific LBS services using the tag of the Android Manifest file.Although this tag is not enforced by the Android operating system, it enables popular publication mechanisms such as the Android Market to filter your app and provide it only to users with appropriate devices. If your application will only function well on devices with some sort of method for determining the current location, you could use the following tag in your application’s manifest file: If your application requires a precise location fix (that is, the device has functional GPS hardware, not just cell tower triangulation or other such mechanisms), you could use the following tag instead: Note New settings for the tag have been added in recent Android SDK releases. For example, the values we’ve discussed, such as android.hardware.location, were added in Android 2.2 (API Level 8).
Finding Your Location
To determine device location, you need to perform a few steps and make some choices. The following list summarizes this process: 1. Retrieve an instance of the LocationManager using a call to the getSystemService() method using the LOCATION_SERVICE. 2. Add an appropriate permission to the AndroidManifest.xml file, depending on what type of location information the application needs. 3. Choose a provider using either the getAllProviders() method or the getBestProvider() method. 4. Implement a LocationListener class. 5. Call the requestLocationUpdates() method with the chosen provider and the LocationListener object to start receiving location information. Specific permissions are not needed to retrieve an instance of the LocationManager object. Instead, the permissions determine the available providers.The following code retrieves an instance of the LocationManager object: Using Global Positioning Services (GPS) 317 import android.location.*; … LocationManager location = (LocationManager)getSystemService(Context.LOCATION_SERVICE); The following block of XML provides the application with both coarse and fine location permissions when added within the AndroidManifest.xml permissions file: Now that the application has permissions to use location information and the LocationManager object is valid, we must determine what provider to use for location information.The following code configures a Criteria object and requests the provider based on this information: Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.NO_REQUIREMENT); criteria.setPowerRequirement(Criteria.NO_REQUIREMENT); String bestProvider = location.getBestProvider(criteria, true); The setAccuracy() method can take values for ACCURACY_COARSE and ACCURACY_FINE that can be used (along with the appropriate permissions) to request a provider that the application has permissions to use.You can use the setPowerRequirement() method to find a provider that fits certain power use requirements, such as POWER_HIGH or POWER_LOW.The Criteria object also enables us to specify if the provider can incur a monetary cost to the user, whether altitude is needed, and some other details. If the application has specific requirements, this is where you set them. However, setting these criteria doesn’t imply that the provider is available to the user. Some flexibility might be required to allow use on a broad range of devices.A Boolean parameter of the getBestProvider() method enables the application to ask for only enabled providers. Using the provider returned by the getBestProvider() method, the application can request the location. Before doing so, however, the application needs to provide an implementation of LocationListener.The LocationListener implementation consists of four methods:To tell the application whether the provider has been disabled or enabled; to give the status about the provider (such as the number of satellites the GPS receiver can see); and to tell the application location information.The following is a sample implementation for the last method, the onLocationChanged() method: public void onLocationChanged(Location location) { String locInfo = String. format(« Current loc = (%f, %f) @ (%f meters up) », location.getLatitude(), location.getLongitude(), location.getAltitude() ); 318 Chapter 14 Using Location-Based Services (LBS) APIs if (lastLocation != null) { float distance = location.distanceTo(lastLocation); locInfo += String. format(« \n Distance from last = %f meters », distance); } lastLocation = location; status.setText(locInfo); } The onLocationChanged() method receives a Location object with the most recent location information from the chosen provider. In this example, the application merely prints out the location, including the altitude, which might or might not be returned by the provider.Then, it uses a utility method of the Location object, distanceTo(), to calculate how far the handset has moved since the last time onLocationChanged() was called. It is up to the application to determine how to use this location information.The application might want to turn the location information into an address, display the location on an embedded map, or launch the built-in Maps application (if the Google applications are installed) centered at the location.