Finding Location Providers Using Criteria

In most scenarios it's unlikely that you will want to explicitly choose the Location Provider to use. More commonly, you'll specify the requirements thtia provider must meet and let Android determine the best technology to use.

Use the Criteria class to dictate the requirements of a provider in terms of accuracy (fine or coarse), power use (low, medium, high), financial cost, and the ability to return values for altitude, speed, and bearing.

Listing 8-1 specifies Criteria requiring coarse accuracy, low power consumption, and no need for altitude, bearing, or speed. The provider is permitted to have an associated cost.

LISTING 8-1: Specifying Location Provider Criteria

Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_COARSE); criteria.setPowerRequirement(Criteria.POWER_LOW); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setSpeedRequired(false); criteria.setCostAllowed(true);

Having defined the required Criteria, you can use getBestProvider to return the best matching Location Provider or getProviders to return all the possible matches. The following snippet demonstrates the use of getBestProvider to return the best provider for your criteria where the Boolean lets you restrict the result to a currently enabled provider:

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

If more than one Location Provider matches your criteria, the one with the greatest accuracy is returned. If no Location Providers meet your requirements the criteria are loosened, in the following order, until a provider is found:

> Power use

> Accuracy

> Ability to return bearing, speed, and altitude

Prepared for ASHLEE KABAT, email: [email protected] Order number: 56760408 This PDF is for the purchaser's personal use in accordance with the Wrox Terms of Service and under US copyright as stated on this book's copyright page. If you did not purchase this copy, please visit www.wrox.com to purchase your own copy.

The criterion for allowing a device with monetary cost is never implicitly relaxed. If no provider is found, null is returned.

To see a list of names for all the providers that match your criteria you can use getProviders. It accepts a Criteria object and returns a filtered String list of all available Location Providers that match them. As with the getBestProvider call, if no matching providers are found, this call returns null.

List<String> matchingProviders = locationManager.getProviders(criteria, false);

FINDING YOUR LOCATION

The purpose of location-based services is to find the physical location of the device.

Access to the location-based services is handled by the Location Manager system Service. To access the Location Manager, request an instance of the LOCATION_SERVICE using the getSystemService method, as shown in the following snippet:

String serviceString = Context.LOCATION_SERVICE; LocationManager locationManager;

locationManager = (LocationManager)getSystemService(serviceString);

Before you can use the Location Manager you need to add one or more uses-permission tags to your manifest to support access to the LBS hardware.

The following snippet shows the fine and coarse permissions. An application that has been granted fine permission will have coarse permission granted implicitly.

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

The GPS provider requires fine permission, while the Network (Cell ID/Wi-Fi) provider requires only coarse.

You can find the last location fix determined by a particular Location Provider using the getLastKnownLocation method, passing in the name of the Location Provider. The following example finds the last location fix taken by the GPS provider:

String provider = LocationManager.GPS_PROVIDER;

Location location = locationManager.getLastKnownLocation(provider);

Note that getLastKnownLocation does not ask the Location Provider to update the current position. If the device has not recently updated the current position, this value may not exist or be out of date.

The Location object returned includes all the position information available from the provider that supplied it. This can include latitude, longitude, bearing, altitude, speed, and the time the location fix was taken. All these properties are available via get methods on the Location object. In some instances additional details will be included in the extras Bundle.

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment