Best location data, regardless of provider?

by Anna PS » Thu, 30 Apr 2009 06:20:00 GMT

Sponsored Links
 i there,

Sorry if this has been answered already - I can't find the answer
either here or in Android Developers.

I want to get the user's location, using the best provider - either
GPS or network. If GPS is not updating, then I want to use the network
provider. If the user is outside and GPS is more accurate, then I want
to use GPS.

Does Android have some built-in method to grab the most accurate + up-
to-date location, from whichever provider?

I've got code working to register the LocationManager with every
available provider, but I don't know how to choose the most accurate
one - short of manually comparing the accuracy and age of the location
data given by each provider. (Which could get a bit complex, because
you don't want to discard data that's only a few seconds older, if
it's more accurate, but you do want to discard data that's more than a
few minutes old...)

Surely there's a simple way of doing this?



public void onCreate(Bundle icicle) {

// Register for location listener
String location_context = Context.LOCATION_SERVICE;
locationmanager = (LocationManager) getSystemService

// Do some stuff

// Now, get the best location from either network or GPS - how?
Location locationGPS =
Location locationNetwork = locationmanager.getLastKnownLocation
// Compare the two...?


public void testProviders() {
Log.e(LOG_TAG, "testProviders");
StringBuilder sb = new StringBuilder("Enabled Providers");
List<String> providers = locationmanager.getProviders(true);
for (String provider : providers) {
listener = new LocationListener() {
public void onLocationChanged(Location
location) {

public void onProviderDisabled(String provider)

public void onProviderEnabled(String provider) {

public void onStatusChanged(String provider,
int status,
Bundle extras) {
locationmanager.requestLocationUpdates(provider, 1000,

sb.append("\n*").append(provider).append(": ");

Location location =

// It's clear from this that the various
providers are quite different in the age & accuracy of their data...
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
float locAccuracy = location.getAccuracy();
long locAge = location.getTime();

Other Threads

1. ==Making "back" return to the app that launched the current activity

I have some results that I am returning to the Android Quick Search
Box.  When a result is selected it takes the user into my app.
However, when the user presses "back" at this point I want them to
return to the Quick Search Box, not to the previous activity in my
app.  Currently I am starting the activity in my app with:

startActivity( newintent );

What do I need to change to make "back" return to the android Quick
Search Box that launched it?



2. My Android device is port scanning, Port 137. Anyone heard of this?

My university's Internet Security has flagged my account numerous
times for port scanning.  Referenced the MAC address to my Archos 10.1
Android Internet Tablet.  It sends a few hundred hits to port 137.

I've tried various antivirus apps but it seems they only cross-
reference installed apps with known malicious apps in the company's
database.  I don't have any malicious apps that I know of; all of them
are well-known apps.

Any idea on how to find and remove whatever is causing this without



3. (update) WTS motorola droid x

4. how to clear all activity.

5. Honeycomb now on the N1!

6. XMLSerializer problem appending

7. OOT-Promo 3