14.01.2013 Views

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7: Maps, Geocoding, and Location-Based Services<br />

Having defi ned the required Criteria, you can use getBestProvider to return the best matching<br />

Location Provider or getProviders to return all the possible matches. The following snippet demonstrates<br />

using getBestProvider to return the best provider for your criteria where the Boolean lets you<br />

restrict the result to a currently enabled provider:<br />

String bestProvider = locationManager.getBestProvider(criteria, true);<br />

If more than one Location Provider matches your criteria, the one with the greatest accuracy is<br />

returned. If no Location Providers meet your requirements, the criteria are loosened, in the following<br />

order, until a provider is found:<br />

❑<br />

❑<br />

❑<br />

Power use<br />

Accuracy<br />

Ability to return bearing, speed, and altitude<br />

The criterion for allowing a device with monetary cost is never implicitly relaxed. If no provider is<br />

found, null is returned.<br />

To see a list of names for all the providers that match your criteria, you can use getProviders. It accepts<br />

Criteria and returns a fi ltered String list of all available Location Providers that match them. As with the<br />

getBestProvider call, if no matching providers are found, this call returns null.<br />

List matchingProviders = locationManager.getProviders(criteria,<br />

false);<br />

Finding Your Location<br />

The purpose of location-based services is to fi nd the physical location of the device.<br />

Access to the location-based services is handled using the Location Manager system Service. To access<br />

the Location Manager, request an instance of the LOCATION_SERVICE using the getSystemService<br />

method, as shown in the following snippet:<br />

String serviceString = Context.LOCATION_SERVICE;<br />

LocationManager locationManager;<br />

locationManager = (LocationManager)getSystemService(serviceString);<br />

Before you can use the Location Manager, you need to add one or more uses-permission tags to your<br />

manifest to support access to the LBS hardware.<br />

The following snippet shows the fi ne and coarse permissions. Of the default providers, the GPS provider<br />

requires fi ne permission, while the Network provider requires only coarse. An application that has<br />

been granted fi ne permission will have coarse permission granted implicitly.<br />

<br />

<br />

213

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!