Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7869491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:20:17+00:00 2026-06-03T01:20:17+00:00

I read the tutorial about Obtaining User Location in Android Dev Guid and, I

  • 0

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"
     }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:20:20+00:00Added an answer on June 3, 2026 at 1:20 am

    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);
            }
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read this tutorial http://tipsandtricks.runicsoft.com/Cpp/BitmapTutorial.html about bitmap and it really helped..I need to read
I read this tutorial about storing images in DB. In the tutorial, the author
I have read the tutorial from ibm about xml parsing (http://www.ibm.com/developerworks/opensource/library/x-android/) In this example,there
I have read the tutorial from ibm about xml parsing (http://www.ibm.com/developerworks/opensource/library/x-android/) In this example,there
I read some tutorial about volatile in the C language, but I still can
I just read this tutorial. It's about game development, and it basically says that
I just learning yii framework and read this tutorial about yii how to setup
I had watched Jose Smith video and read some tutorial about MVVM, but I
I have just read this [very nice] small tutorial about MYSQL error handling. I
few days ago i read tutorial about GenericRepository and Unit Of Work patterns http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.