11

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?

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
Or Kazaz
  • 549
  • 1
  • 9
  • 16
  • http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-a/3145655#3145655. Check the link. You may find answers to your questions. – Raghunandan Oct 16 '12 at 17:52
  • This still doesn't answer my question. My code also works. But should I or should I not call the getBestProvider each time? In my opinion yes.. – Or Kazaz Oct 17 '12 at 16:29
  • http://developer.android.com/guide/topics/location/strategies.html#BestEstimate. You probably should go through this link. – Raghunandan Oct 17 '12 at 17:30

2 Answers2

1

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

Or Kazaz
  • 549
  • 1
  • 9
  • 16
0

You could always use this , but make sure to check on the Location.getTime() -> this would tell you the time it was fixed and so you can easily differentiate between the fresh and stale location fixes.

cafebabe1991
  • 4,928
  • 2
  • 34
  • 42