The starting point of the API use is the LocationProvider
class,
which represents the location provider module. An instance of the LocationProvider
class
can be created by using the factory method LocationProvider.getInstance(Criteria
criteria)
. Criteria parameters can be used to define what kind
of location provider is accepted. The Location API implementation chooses
the location provider that best matches the defined criteria. The TouristRoute
example defines a set of cost criteria that are used in a location provider
search process.
Code sample 1: Creating criteria (TouristRoute's ConfigurationProvider.java class)
Criteria crit1 = new Criteria(); crit1.setHorizontalAccuracy(25); // 25m crit1.setVerticalAccuracy(25); // 25m crit1.setPreferredResponseTime(Criteria.NO_REQUIREMENT); crit1.setPreferredPowerConsumption(Criteria.NO_REQUIREMENT); crit1.setCostAllowed(false); crit1.setSpeedAndCourseRequired(true); crit1.setAltitudeRequired(true); crit1.setAddressInfoRequired(true);
Code sample 2: Choosing the location provider (TouristRoute's ConfigurationProvider.java class)
A code snippet from the location provider search process can be seen
in the Code sample 2 below. See the ConfigurationProvider
class
code in the TouristRoute MIDlet for a complete code that includes more logic.
try provider = LocationProvider.getInstance(criteria); if (provider != null) { // provider found! // .. see the complete source code for the real action. } catch(LocationException le) // LocationProviders are currently out of service
If the device does not have any built-in location methods available
or any external methods connected to it, the LocationProvider.getInstance()
method
throws LocationException
, and the TouristRoute MIDlet
gives a notification that says all providers are out of service. The location
provider is found when the result from LocationProvider.getInstance()
is
not null
. The TouristRoute MIDlet continues the search
in a loop until one location-providing method is found.