realtà Android Developers - Making Your App Location Aware ha un esempio di codice grande per soddisfare le vostre esigenze.
nel suo codice, se si utilizzano entrambi i fornitori (dal GPS e dalla rete) farà un confronto:
...
} 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);
}
}
...
Si attuare l'idea di migliori fornitori di posizione (da determinare la precisione entro un determinato termine di tempo come:
/** 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;
}
Per il codice completo, si prega di dare un'occhiata al link fornito sopra
se si utilizza la classe di Criteri per trovare il miglior Provider disponibile in base alla configurazione, ne risulterà qualcuno da PROVIDER GPS, FORNITORE DI RETE o PASSIVO. e senza la classe Criteria utilizzare requestLocationUpdates per ottenere l'accodazione della posizione al provider –
LocationManager non mi notificherà su rete e GPS in una sola callback? Devo usare due requestLocation() con diversi callback? – Nik
Per registrare il gestore località per ottenere aggiornamenti, è necessario fornire un provider da cui si desidera ottenere gli aggiornamenti. come 'mgr.requestLocationUpdates (LocationManager.NETWORK_PROVIDER, 10 * 1000, 50, this);' –