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. iPhone worshipper rejoice!

Horee....
Sekarang kalian , penyayang iPhone bisa mengunduh {*filter*}o aplikasi di
iPhone....
Berubahlah jadi android, dan download mikandi!

Iphone running android

 http://www.***.com/ ;feature=youtube_gdata

Salam,

Setip Jop

-- 
"Indonesian Android Community [id-android]" 

Join:  http://www.***.com/ 
Moderator: id.andr...@gmail.com
ID Android Developer:  http://www.***.com/ 
ID Android Surabaya:  http://www.***.com/ 
ID Android on FB:  http://www.***.com/ 

2. SOLD..WTS : HTC Desire BNIB (1unit).

Just update ;)

Sold to mr.N :D *tebak siapa*
Qeqeqqqqqq....

es-bile.: 021-33111998
: sa...@yes-mobile.net

-----Original Message-----
From: "Tio.Ping.quot; <tio.ping.andr...@yes-mobile.net>
Date: Thu, 22 Apr 2010 00:55:01 
To: Id-android<id-android@googlegroups.com>; 
Facedroid<facedroid...@googlegroups.com>
Subject: WTS : HTC Desire BNIB (1unit).


jual santai :D
gray. 
barang ready hari minggu. 
barang cuma 1unit.
harga japri aja ;) @siggy.

es-bile.: 021-33111998
: sa...@yes-mobile.net

-- 
"Indonesian Android Community [id-android]" 

3. HTC Hero Sprint 2n

4. Routing Audio data to Both HEADSET and SPEAKER.

5. No place in Google Checkout to set international tax rates. Do we owe? How to Pay?

6. AlertDialog - selected item

7. Statusbar source code in android