Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use android LocationManager.getBestProvider every time I ask for location updates?

I'm using Google APIs and the MapActivity, MapView etc..
When I need to get my current location I use this code the FIRST TIME :

myLocationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
// Sets the criteria for a fine and low power provider
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
crit.setPowerRequirement(Criteria.POWER_LOW);    
// Gets the best matched provider, and only if it's on
String provider = myLocationManager.getBestProvider(crit, true);
// If there are no location providers available
if (provider == null){
    OnClickListener listener = new OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // Closes the activity
            finish();
        }
    };
    Utils.getInstance().showAlertDialog(this, R.string.warning_dialog_title,
                               R.string.no_location_providers_error_message,
                            android.R.drawable.ic_dialog_alert, false, listener);
}
// Location services is enabled
else{
    myLocationListener = new MyLocationListener();
    myLocationManager.requestLocationUpdates(
            provider,
            0,
            0,
            myLocationListener);        
    // TODO - put a marker in my location       
    GeoPoint currentGeoPoint = MapUtils.getInstance().
            getCurrentLocation(myLocationManager, provider);        
    // Center to the current location
    centerLocation(currentGeoPoint, MAX_ZOOM);
}

I also have a "back to current location button" - for when the user moves the map around and wants to quickly return to his current location.

My question is, should I use the getBestProvider() method each time I want to get location information?

I could save the last choosen provider, but the conditions could change every second:

  1. User turned off GPS
  2. No GPS signal
  3. NETWORK_PROVIDER is better in the current situation
  4. etc..

What is the right approach?

like image 691
Or Kazaz Avatar asked Oct 16 '12 17:10

Or Kazaz


People also ask

What is the use of location manager in Android?

These services allow applications to obtain periodic updates of the device's geographical location, or to be notified when the device enters the proximity of a given geographical location.

Which method is used to list all the location providers in Android?

They are: gps –> (GPS, AGPS): Name of the GPS location provider. This provider determines location using satellites. Depending on conditions, this provider may take a while to return a location fix.

How do I turn off Location listener on Android?

removeUpdates(locationListenerObject); mLocManager = null; Call this inside your onLocationChange Method when lat and long has been captured by the listener. It might take some time for the onLocationChange method to be called.


1 Answers

Like been told on the coments, this is the answer: http://developer.android.com/guide/topics/location/strategies.html#BestEstimate

like image 188
Or Kazaz Avatar answered Oct 15 '22 09:10

Or Kazaz