Geolocation - How to implement android documentation code for comparing multiple location results

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

http://blog.doityourselfandroid.com/2010/12/25/understanding-locationlistener-android/ http://blog.doityourselfandroid.com/2010/12/25/understanding-locationlistener-android/

http://developer.android.com/guide/topics/location/strategies.html http://developer.android.com/guide/topics/location/strategies.html

I am confused on how to implement the block of code from that section into my own app. The block of code takes two parameters, location and currentbestlocation and compares them.

    • How do you declare two location objects for the block of code to compare? (code example or pseudo code would be much appreciated!)
    • Is my understanding correct that locationlistener provides multiple location objects?
    • How do I implement GOOGLES code into mine? See below:

my code below:

public class MainActivity extends Activity {
    LocationManager lm;
    LocationListener ll;
    private Location previousLocation;

    public void onCreate(Context context, Intent intent) {    
        lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        ll = new myListener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 1000, ll);
    }

    private class myListener implements LocationListener {      
        public void onLocationChanged(Location loc) {
        if (previousLocation == null) {
               previousLocation = loc;
            } else {
                if (isBetterLocation(loc, previousLocation)) {
                //NOTIFICATION NEW LOCATION IS BETTER
                } else {
                //NOTIFICATION PREVIOUS LOCATION IS BETTER
                }
            }
        }
        public void onProviderDisabled(String provider) {
        }
        public void onProviderEnabled(String provider) {
        }
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }

    //GOOGLE ANDROID DOCUMENTATION CODE FOR MAINTAINING CURRENT BEST ESTIMATE
    private static final int TWO_MINUTES = 1000 * 60 * 2;

    protected boolean isBetterLocation(Location location, Location currentBestLocation) {
        if (currentBestLocation == null) {
            // A new location is always better than no location
            return true;
        }

        // Check whether the new location fix is newer or older
        long timeDelta = location.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 true;
        // If the new location is more than two minutes older, it must be worse
        } else if (isSignificantlyOlder) {
            return false;
        }

        // Check whether the new location fix is more or less accurate
        int accuracyDelta = (int) (location.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(location.getProvider(), currentBestLocation.getProvider());

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

    private boolean isSameProvider(String provider1, String provider2) {
        if (provider1 == null) {
            return provider2 == null;
        }
            return provider1.equals(provider2);
    }
}

Answers

When you use the LocationManager to request location updates, it will continue giving you updates until you tell it to stop or your application is killed. This way, you can monitor the device s current location at some interval so you know when they ve moved. If you only need a single location update, once you ve received a location fix that is of sufficient accuracy, call locationManager.removeUpdates(listener).

http://developer.android.com/reference/android/location/LocationManager.html#requestSingleUpdate%28java.lang.String,%20android.location.LocationListener,%20android.os.Looper%29 http://developer.android.com/reference/android/location/LocationManager.html#requestSingleUpdate%28java.lang.String,%20android.location.LocationListener,%20android.os.Looper%29

The first thing to do is declare your location listener and register for updates. This code should get your started:

public class MyActivity extends Activity implements LocationListener {
    /** Cache the last location fix received. */
    private Location mLastLocationReceived;

    @Override
    public void onResume() {
        super.onResume();

        // Register our location listener
        LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 500, this);
    }

    @Override
    public void onPause() {
        super.onPause();

        // Unregister our location listener
        LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        lm.removeUpdates(this);
    }

    @Override
    public void onLocationChanged(Location location) {
        if (mLastLocationReceived == null) {
            mLastLocationReceived = location;
        } else {
            if (isBetterLocation(location, mLastLocationReceived)) {
                // New location fix is better!
            } else {
                // New location fix is not better!
            }
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
        // Pass
    }

    @Override
    public void onProviderEnabled(String provider) {
        // Pass
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // Pass
    }

    private boolean isBetterLocation(Location newLocation,
            Location oldLocation) {

        // TODO: Implement the logic to determine if the new location is
        // of better quality than the old location. Your application s
        // business logic determines what this method should do.

        return false;
    }

}

http://developer.android.com/guide/topics/location/strategies.html#BestEstimate http://developer.android.com/guide/topics/location/strategies.html#BestEstimate

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/14105966/how-to-implement-android-documentation-code-for-comparing-multiple-location-resu

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils