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. Send Data to a running app

I have a broadcast reciever the looks at SMS's, when it recieves an
SMS that is destined for my application it needs to send some data to
an activity or start the avitivty if it isnot running.

How do i find out if the activity is running and send data to it if it
is? I know i can use StartActivity and stuff the intent with the data
i want to send to it but if i start the activty that is already
running, another instance of it will be created (wont it?) .

Thanks for any info.



2. Application fails only on Samsung Behold II

I'm the author of "Alarm Clock Plus V1" and "Alarm Clock Plus V2" in
the android market. They are basically enhanced alarm clocks built
using the stock android alarm clock apps from 2.0, and 2.1.

I've done all the work making them compatible with android 1.5+ and I
know the app is working on many G1's, myTouch's, CLIQ's, and various
other 1.5/1.6/2.1/2.2 android devices.

However the Samsung Behold II has problems with the application.
Specifically it can't edit any of the alarms. I'm looking for someone
out there that may have any idea as to what the problem could be. The
only thing I can possibly think of is that the application can read
from the app's sqlite database, but not write to it (but that's
honestly a shot in the dark).

Any help out there?


3. google gears in webview! (works only in browser)

4. Moving from one Activity to another with a pause/delay

5. WebView not loading enough JavaScript that the full Android Browser can.

6. ProgressDialog BackGround

7. About ListView filter