Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class LocationFetcher {
private LocationManager mLocationManager;
private LocationListener mListener;
private boolean mInProgress;
public LocationFetcher(LocationManager locationManager, OnLocationFetchedCallback callback) {
mLocationManager = Objects.requireNonNull(locationManager);
mListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
callback.onLocationFetched(ll);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
/* no-op */
}
@Override
public void onProviderEnabled(String provider) {
/* no-op */
}
@Override
public void onProviderDisabled(String provider) {
/* no-op */
}
};
}
@SuppressLint("MissingPermission")
public void start() {
mInProgress = true;
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 200, 0, mListener);
}
public void stop() {
mInProgress = false;
mLocationManager.removeUpdates(mListener);
}
public boolean isInProgress() {
return mInProgress;
}
public interface OnLocationFetchedCallback {
void onLocationFetched(LatLng location);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.