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?

thanks!
Anna

-------------------------

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

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

// Do some stuff

// Now, get the best location from either network or GPS - how?
Location locationGPS =
locationmanager.getLastKnownLocation("gps");
Location locationNetwork = locationmanager.getLastKnownLocation
("network");
// 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,
0,
listener);

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

Location location =
locationmanager.getLastKnownLocation(provider);

// 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. "MiB" in "Create new Android Virtual Device"

This may sound like a stupid question, but what the heck does "MiB"
stand for in the "Create new Android Virtual Device" dialog in
Eclipse?  It -seems- like it means "megabyte", but in my 29 years of
programming, not once have I ever seen "MiB" as an acronym for
megabyte.  It's aways been MB and sometimes "Meg" and sometimes
incorrectly as "Mb" or "mb", but never "MiB".  That's why I'm asking,
and since I'm a newb to Android (not newb to programming) I'm thinking
maybe this is something Android specific and means something else
entirely.

Thanks.

-- 

2. onCreate() vs class variable initializers

It seems that many actions can be performed in either the onCreate()
method for an activity or in the variable initializer section at the
beginning of the class definition.  For example,  I could write:

     public class Main extends Activity {
        private Handler handler = new Handler();
        . . .

or I could write:

     public class Main extends Activity {
        private Handler handler;

     public void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
         handler = new Handler();
         . . .

What are the considerations that would make me want to do the
initialization in one place rather than the other?  Both forms of
initialization happen exactly once whenever the class is instantiated,
right?  And the variable initializers at the top happen before the
code in onCreate() is run.  But are there any other considerations as
to where I should initialize values for variables?

-- 

3. How to get this information (In this picture) to TextView on Android ?

4. Magnetic Sensor

5. [WTA] : Disable Themes ADW

6. JUnit test case blocking main UI thread for AsyncTask

7. How to initialize visibly "gone" UI elements?