Android LocationManager Criteria
Asked Answered
P

3

8

I need to receive location changes both from Network and GPS providers.

If GPS provider not avaliable or has not location (bad sattelite visibility) I would receive location from Network provider else from GPS provider.

Is it possible to select provider using criteria according to my necessity?

Preadamite answered 28/3, 2013 at 10:17 Comment(3)
if you are using Criteria class for finding best Provider available according to configuration then will result anyone from GPS PROVIDER , NETWORK PROVIDER or PASSIVE. and without Criteria class use requestLocationUpdates for getting location accoding to providerPecoraro
LocationManager will not notify me about Network and GPS both in one callback? Need I to use two requestLocation() with different callbacks?Preadamite
To register your location manager for getting updates, you must give a provider, from whom you want to get updates. like mgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10 * 1000, 50, this);Gluck
F
10

Actually Android Developers - Making Your App Location Aware has a great example code to meet your needs.

in its code, if you use both providers (from GPS and from Network) it will do a comparison:

...
} else if (mUseBoth) {
    // Get coarse and fine location updates.
    mFineProviderButton.setBackgroundResource(R.drawable.button_inactive);
    mBothProviderButton.setBackgroundResource(R.drawable.button_active);
    // Request updates from both fine (gps) and coarse (network) providers.
    gpsLocation = requestUpdatesFromProvider(
            LocationManager.GPS_PROVIDER, R.string.not_support_gps);
    networkLocation = requestUpdatesFromProvider(
            LocationManager.NETWORK_PROVIDER, R.string.not_support_network);

    // If both providers return last known locations, compare the two and use the better
    // one to update the UI.  If only one provider returns a location, use it.
    if (gpsLocation != null && networkLocation != null) {
        updateUILocation(getBetterLocation(gpsLocation, networkLocation));
    } else if (gpsLocation != null) {
        updateUILocation(gpsLocation);
    } else if (networkLocation != null) {
        updateUILocation(networkLocation);
    }
}
...

It implement the idea of best location providers (by determine the accuracy within a specified period of time like:

/** Determines whether one Location reading is better than the current Location fix.
  * Code taken from
  * http://developer.android.com/guide/topics/location/obtaining-user-location.html
  *
  * @param newLocation  The new Location that you want to evaluate
  * @param currentBestLocation  The current Location fix, to which you want to compare the new
  *        one
  * @return The better Location object based on recency and accuracy.
  */
protected Location getBetterLocation(Location newLocation, Location currentBestLocation) {
    if (currentBestLocation == null) {
        // A new location is always better than no location
        return newLocation;
    }

    // Check whether the new location fix is newer or older
    long timeDelta = newLocation.getTime() - currentBestLocation.getTime();
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
    boolean isNewer = timeDelta > 0;

    // If it's been more than two minutes since the current location, use the new location
    // because the user has likely moved.
    if (isSignificantlyNewer) {
        return newLocation;
    // If the new location is more than two minutes older, it must be worse
    } else if (isSignificantlyOlder) {
        return currentBestLocation;
    }

    // Check whether the new location fix is more or less accurate
    int accuracyDelta = (int) (newLocation.getAccuracy() - currentBestLocation.getAccuracy());
    boolean isLessAccurate = accuracyDelta > 0;
    boolean isMoreAccurate = accuracyDelta < 0;
    boolean isSignificantlyLessAccurate = accuracyDelta > 200;

    // Check if the old and new location are from the same provider
    boolean isFromSameProvider = isSameProvider(newLocation.getProvider(),
            currentBestLocation.getProvider());

    // Determine location quality using a combination of timeliness and accuracy
    if (isMoreAccurate) {
        return newLocation;
    } else if (isNewer && !isLessAccurate) {
        return newLocation;
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
        return newLocation;
    }
    return currentBestLocation;
}

For complete code, please have a look at the link provided above.

Fructiferous answered 28/3, 2013 at 10:57 Comment(0)
A
2

For search best location Provider please user belowe code : Please refer https://developer.android.com/reference/android/location/Criteria.html for understand, How criteria will going to work ?

public  String getProviderName() {
    LocationManager locationManager =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setSpeedRequired(true);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(false);
    return locationManager.getBestProvider(criteria, true);
}
Alfons answered 5/12, 2016 at 12:47 Comment(1)
Criteria is already deprecatedImperialism
G
0

Just don't give any criteria, and try to get the best provider:

LocationManager mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String best = mgr.getBestProvider(criteria, true);
//since you are using true as the second parameter, you will only get the best of providers which are enabled.
Location location = mgr.getLastKnownLocation(best);
Gluck answered 28/3, 2013 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.