Skip to content

Instantly share code, notes, and snippets.

@jonahwilliams
Created June 24, 2018 17:03
Show Gist options
  • Save jonahwilliams/4f7463acf7114046bde00a54c191b7f9 to your computer and use it in GitHub Desktop.
Save jonahwilliams/4f7463acf7114046bde00a54c191b7f9 to your computer and use it in GitHub Desktop.
import 'dart:async';
class PositionTracker {
PositionTracker(this._currentLat, this._currentLong, [this.minimumDelta = 100.0]);
final double minimumDelta;
final StreamController<void> _onUpdate = new StreamController<void>();
double _currentLat;
double _currentLatDelta = 0.0;
double _currentLong;
double _currentLongDelta = 0.0;
Stream<void> get onUpdated => _onUpdate.stream;
double get lat => _currentLat;
double get long => _currentLong;
void updatePosition(double lat, double long) {
// compute delta somehow, pretty sure this isn't correct XD
double deltaLat = (_currentLat - lat).abs();
double deltaLong = (_currentLong - long).abs();
_currentLat = lat;
_currentLong = long;
_currentLatDelta += deltaLat;
_currentLongDelta += deltaLong;
// Probablt not a linear combination, but thats not the point!
if (_currentLatDelta + _currentLongDelta > minimumDelta) {
_onUpdate.add(null);
_currentLatDelta = 0.0;
_currentLongDelta = 0.0;
}
}
}
Future<void> main() async {
final PositionTracker tracker = new PositionTracker(0.0, 10.0);
tracker.onUpdated.listen((_) => print('updated!'));
tracker.updatePosition(100.0, 200.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment