Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do the location and currentBestLocation come from? in Android Dev Guide (Obtaining User Location)

I read the tutorial about Obtaining User Location in Android Dev Guid and, I try to adapt this to the following code.. but i don't know which location value I should put into isBetterLocation(Location location, Location currentBestLocation)

Example.class

       private LocationManager locman;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

            String context = Context.LOCATION_SERVICE;
            locman = (LocationManager)getSystemService(context);

            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setAltitudeRequired(false);
            criteria.setBearingRequired(false);
            criteria.setPowerRequirement(Criteria.POWER_LOW);

            String provider = locman.getBestProvider(criteria, true);
            locman.requestLocationUpdates(
                    provider,MIN_TIME, MIN_DISTANCE, locationListener);

        }

        private LocationListener locationListener = new LocationListener(){

        @Override
        public void onLocationChanged(Location location) {
            // What should i pass as first and second parameter in this method
            if(isBetterLocation(location1,location2)){
               // isBetterLocation = true > do updateLocation 
               updateLocation(location);
            }
        }

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

       };

     protected boolean isBetterLocation(Location location, Location currentBestLocation) {
         if (currentBestLocation == null) {
            // A new location is always better than no location
            return true;
         }
          //Brief ... See code in Android Dev Guid "Obtaining User Location"
     }
like image 902
April Smith Avatar asked Apr 26 '12 06:04

April Smith


1 Answers

Its not that hard really. What the code does is it receives continuous updates on locations found; you can have multiple listeners listening to different providers and as such those updates can be more or less accurate depending on the provider (GPS for example could be more accurate than network). isBetterLocation(...) evaluates if a location found by the listener is actually better than the one you already know about (and should have a reference to in your code). The isBetterLocation(...) code is well documented, so it shouldn't be hard to understand, but the first parameter location is the new location found by a provider, and currentBestLocation is the location you already know about.

The code I use is about the same as yours, except I don't just take best provider. The handler stuff is because I don't want continued updates, just find the best possible location that is accurate enough for me within a maximum timeframe of two minutes (GPS can take a bit).

private Location currentBestLocation = null;
private ServiceLocationListener gpsLocationListener;
private ServiceLocationListener networkLocationListener;
private ServiceLocationListener passiveLocationListener;
private LocationManager locationManager;

private Handler handler = new Handler();


public void fetchLocation() {
    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    try {
        LocationProvider gpsProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER);
        LocationProvider networkProvider = locationManager.getProvider(LocationManager.NETWORK_PROVIDER);
        LocationProvider passiveProvider = locationManager.getProvider(LocationManager.PASSIVE_PROVIDER);

        //Figure out if we have a location somewhere that we can use as a current best location
        if( gpsProvider != null ) {
            Location lastKnownGPSLocation = locationManager.getLastKnownLocation(gpsProvider.getName());
            if( isBetterLocation(lastKnownGPSLocation, currentBestLocation) )
                currentBestLocation = lastKnownGPSLocation;
        }

        if( networkProvider != null ) {
            Location lastKnownNetworkLocation = locationManager.getLastKnownLocation(networkProvider.getName());
            if( isBetterLocation(lastKnownNetworkLocation, currentBestLocation) )
                currentBestLocation = lastKnownNetworkLocation;
        }

        if( passiveProvider != null) {
            Location lastKnownPassiveLocation = locationManager.getLastKnownLocation(passiveProvider.getName());
            if( isBetterLocation(lastKnownPassiveLocation, currentBestLocation)) {
                currentBestLocation = lastKnownPassiveLocation;
            }
        }

        gpsLocationListener = new ServiceLocationListener();
        networkLocationListener = new ServiceLocationListener();
        passiveLocationListener = new ServiceLocationListener();

        if(gpsProvider != null) {
            locationManager.requestLocationUpdates(gpsProvider.getName(), 0l, 0.0f, gpsLocationListener);
        }

        if(networkProvider != null) {
            locationManager.requestLocationUpdates(networkProvider.getName(), 0l, 0.0f, networkLocationListener);
        }

        if(passiveProvider != null) {
            locationManager.requestLocationUpdates(passiveProvider.getName(), 0l, 0.0f, passiveLocationListener);
        }

        if(gpsProvider != null || networkProvider != null || passiveProvider != null) {
            handler.postDelayed(timerRunnable, 2 * 60 * 1000);
        } else {
            handler.post(timerRunnable);
        }
    } catch (SecurityException se) {
        finish();
    }
}

private class ServiceLocationListener implements android.location.LocationListener {

    @Override
    public void onLocationChanged(Location newLocation) {
        synchronized ( this ) {
            if(isBetterLocation(newLocation, currentBestLocation)) {
                currentBestLocation = newLocation;

                if(currentBestLocation.hasAccuracy() && currentBestLocation.getAccuracy() <= 100) {
                    finish();
                }
            }
        }
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {}

    @Override
    public void onProviderEnabled(String s) {}

    @Override
    public void onProviderDisabled(String s) {}
}

private synchronized void finish() {
    handler.removeCallbacks(timerRunnable);
    handler.post(timerRunnable);
}

/** Determines whether one Location reading is better than the current Location fix
 * @param location  The new Location that you want to evaluate
 * @param currentBestLocation  The current Location fix, to which you want to compare the new one
 */
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
    //etc
}

private Runnable timerRunnable = new Runnable() {

    @Override
    public void run() {
        Intent intent = new Intent(LocationService.this.getPackageName() + ".action.LOCATION_FOUND");

        if(currentBestLocation != null) {
            intent.putExtra(LocationManager.KEY_LOCATION_CHANGED, currentBestLocation);

            locationManager.removeUpdates(gpsLocationListener);
            locationManager.removeUpdates(networkLocationListener);
            locationManager.removeUpdates(passiveLocationListener);
        }
    }
};
like image 93
MrJre Avatar answered Sep 22 '22 16:09

MrJre