Finding Yourself

The obvious thing to do with a location service is to figure out where you are right now.

To do that, you need to get a LocationManager - call getSystemService( location_service ) from your activity or service and cast it to be a LocationManager.

The next step to find out where you are is to get the name of the LocationProvider you want to use. Here, you have two main options:

1. Ask the user to pick a provider

2. Find the best-match provider based on a set of criteria

If you want the user to pick a provider, calling getProviders() on the LocationManager will give you a List of providers, which you can then wrap in an ArrayAdapter and use for the selection widget of your choice. The catch is that LocationProvider does not have a useful tostring() implementation, so you need to do a little extra work, either overriding ArrayAdapter to populate your views by hand, or wrapping each LocationProvider in your own object that implements toString() by calling the provider's getName() method. TourIt takes the latter approach in ConfigActivity:

(Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout. config);

LocationManager mgr=(LocationManager)getSystemService(Context.LOCATION_SERVICE);

for (LocationProvider p : mgr.getProviders()) { listWrappers.add(new ProviderWrapper(p));

ArrayAdapter<Waypoint> aa=new ArrayAdapter<Waypoint>(this,

R.layout.spinner, (List)listWrappers);

providers=(Spinner)findViewById(R.id.providers);

aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); providers.setAdapter(aa);

where ProviderWrapper is:

class ProviderWrapper { LocationProvider p;

ProviderWrapper(LocationProvider p) { this.p=p;

public String toString() { return(p.getName());

Or, you can create and populate a Criteria object, stating the particulars of what you want out of a LocationProvider, such as:

• setAltitudeRequired() to indicate if you need the current altitude or not

• setAccuracy() to set a minimum level of accuracy, in meters, for the position

• setCostAllowed() to control if the provider must be free or if it can incur a cost on behalf of the device user

Given a filled-in Critieria object, call getBestProvider() on your LocationManager, and Android will sift through the criteria and give you the best answer. Note that not all of your criteria will be met - all but the monetary cost criterion might be relaxed if nothing matches.

Once you know the name of the LocationProvider, you can call getcurrentLocation() to turn on the location provider and get an up-to-date fix, or you can call getLastKnownPosition() to find out where you were recently. Note, however, that "recently" might be fairly out of date (e.g., phone was turned off). On the other hand, getLastKnownPosition() incurs no monetary or power cost, since the provider does not need to be activated to get the value.

These methods return a Location object, which can give you the latitude and longitude of the device in degrees as a Java double. If the particular location provider offers other data, you can get at that as well:

• For altitude, hasAltitude() will tell you if there is an altitude value, and getAltitude() will return the altitude in meters.

• For bearing (i.e., compass-style direction), hasBearing() will tell you if there is a bearing available, and getBearing() will return it as degrees east of true north.

• For speed, hasSpeed() will tell you if the speed is known and getspeed() will return the speed in meters per second.

For example, TourEditActivity allows users to click a button to fill in the current location when editing waypoint - the theory being that the user is riding the tour and taking locations along the way to update the otherwise-complete tour definition. The user's preferred location provider is stored in a preference, filled in by the ConfigActivity and updated in TourEditActivity's onResume():

(^Override public void onResume() { super.onResume();

SharedPreferences prefs=getSharedPreferences(ConfigActivity.PREFS, 0);

String providerName=prefs.getString(ConfigActivity.LOCATION_PROVIDER, null);

if (providerName!=null) {

for (LocationProvider p : myLocationManager.getProviders()) { if (p.getName().equals(providerName)) { provider=p; break;

Criteria crit=new Criteria();

crit.setCostAllowed(true); crit.setSpeedRequired(false); crit.setBearingRequired(false); crit.setAltitudeRequired(false);

provider=myLocationManager.getBestProvider(crit);

Then, when the button is clicked, it gets a current fix and fills in the location in the appropriate fields:

Button btn=(Button)findViewById(R.id.fillin);

btn.setOnClickListener(new View. OnClickListener() {

public void onClick(View view) { if (provider!=null) {

Location loc=myLocationManager.getCurrentLocation(provider.getName());

pt_lat.setText(new Double(loc.getLatitude()).toString()); pt_long.setText(new Double(loc.getLongitude()).toString()); pt_ele.setText(new Double(loc.getAltitude()).toString());

Self Publishing

Self Publishing

Have you always wanted to write your own book and get it published? Discover How to Write, Print Sell Your Own Book! Have you always wanted to write your own book and get it published? If you have written a book or even if you have a yen to be a writer, you are probably already aware of the competition in the writing field.

Get My Free Ebook


Post a comment