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. Multiple calls to SimpleCursorAdapater#bindView

Hi all, I have a ListView populated using a SimpleCursorAdapter that
has a ViewBinder, and I'm finding that when the ListView is displayed,
ViewBinder#setViewValue is called 3 times for each row of the cursor.

Once during onMeasure:
        at au.com.xandar.contactsmanager.DisplayContactsActivity
$1.setViewValue(DisplayContactsActivity.java:56)
        ...
        at android.view.View.measure(View.java:8171)
        at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
        at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:4627)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:521)
        at com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:868)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
626)
        at dalvik.system.NativeStart.main(Native Method)

Once during onLayout:
        at au.com.xandar.contactsmanager.DisplayContactsActivity
$1.setViewValue(DisplayContactsActivity.java:56)
        ...
        at android.view.View.layout(View.java:7035)
        at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
        at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:4627)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:521)
        at com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:868)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
626)
        at dalvik.system.NativeStart.main(Native Method)

And the third time from onMeasure again.

Firstly, is this expected behaviour?

If it is, then what's the best way of performing potentially expensive
work in ViewBinder#setViewValue?
For example, retrieving the results of another cursor whcih is used to
construct the bound view.

Or am I approaching this from entirely the wrong view point?

William

-- 

2. forcing a view to measure off screen

I have a custom view i am writing for my drawing app - but the first
time i set the drawing into it i need to get the dimension for the
view (using getMeasuredWidth() & getMeasuredHeight()). But until the
view display these return 0. I have tried calling:
requestLayout()
invalidate()
numDraw.measure(MeasureSpec.EXACTLY, MeasureSpec.EXACTLY);

but nothing seems to force the values to update.

I guess the question i am asking is : does anyone have any tips on how
to get view to update its measurements off screen?

best
rob m

-- 

3. Omap4430 ES1.0 boot init error

4. Get a view with findViewById()

5. How to use NDK on Windows

6. iMX51 NFS Mount Issue

7. Logcat don't work anymore...