Skip to content

(Android) Getting location update

Andrzej M edited this page Mar 26, 2014 · 1 revision

LocationUpdater

LocationUpdater is singleton, which take care of connecting with location provider and getting frequently location updates. Whenever you need coordination updates all you have to do is bind to LocationUpdater for them. When updates are needed no more you should always unbind to save battery life.

Getting an instance of LocationUpdater

Typically for singleton pattern, in order to get instance of LocationUpdater you should call: LocationUpdater.getInstance();

Bind and unbind for update

To receive update, you should bind for it using startUpdating(LocationUser locationUser) method, for example :
LocationUpdater.getInstance().startUpdating(mLocationUser);
When you do so, each time new location is available LocationUpdater will invoke mLocationUser.onLocationUpdate(newLocation) (see below).
You can register more LocationUser simultaneously, each of them will receive updates.

To unbind call stopUpdating(LocationUser locationUser), e.g.:
LocationUpdater.getInstance().stopUpdating(mLocationUser);

Get location

To get most recent location currently available call
LocationUpdater.getInstance().getLocation();
Location can be outdated, when no one bind for updates. When at least one LocationUser is currently using LocationUpdater location should be correct.

LocationUser

When you bind for updates you must deliver object which implements LocationUser to provide callbacks. It has 3 methods:
public void onLocationUpdate(Location location)
Invoke after LocationUpdater receive new location from device services, passing current location.
public void onGpsAvailable()
Invoke after gps turned on. All location base UI should be enable within this method.
onGpsAvailable()
Invoke after gps turned off or connection with location services is dropped. All location base UI should be disable within this method.

LocationLogger

Is simple class, which implements LocationUser, that can be use for gps testing. It log and toast current location. To avoid logging too many information it logs coordinates only if the location has been changed. To create it call:
LocationLogger(Context context, int intervalInSec, boolean showToast)
where intervalInSec is minimal number of seconds between logs
and showToast determines if toast should also be show (if true, toasts will be shown)

Simple example

In practice, using LocationUpdater is very simple. Short example should clear everything up.

public class OptionsActivity extends Activity implements LocationUser{

	private Location mLocation;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_options);
    }
    
	@Override
	protected void onStart() {
		super.onStart();
		LocationUpdater.getInstance().startUpdating(this);
	}

	@Override
	protected void onStop() {
		super.onStop();
		LocationUpdater.getInstance().stopUpdating(this);
	}

	@Override
	public void onLocationUpdate(Location location) {
		// React on updates
		mLocation = location;
	}

	@Override
	public void onGpsAvailable() {
		// enable gps functionality 
	}

	@Override
	public void onGpsUnavailable() {
		// disable GUI functionality
	} 
}