Skip to content

Commit

Permalink
Improve location indicator and camera tracking (#280)
Browse files Browse the repository at this point in the history
- fix location indicator not being visible if permission were granted afterwards
- sync camera tracking
- keep camera tracking on zoom changes
- leverage built-in location indicator functionality
- improve map animations to only alter given properties
- don't cancel camera tracking on map rotation events
  • Loading branch information
Robbendebiene authored Jul 31, 2024
1 parent 9b85589 commit a026085
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 260 deletions.
171 changes: 0 additions & 171 deletions lib/api/user_location_service.dart

This file was deleted.

9 changes: 6 additions & 3 deletions lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,12 @@ class HomeScreen extends View<HomeViewModel> with PromptHandler {
),
Observer(
builder: (context) {
// rebuild location indicator when location access is granted
viewModel.userLocationState;
return const AnimatedLocationLayer();
return AnimatedLocationLayer(
controller: viewModel.locationIndicatorController,
cameraTrackingMode: viewModel.cameraIsFollowingLocation
? CameraTrackingMode.location
: CameraTrackingMode.none,
);
},
),
Observer(
Expand Down
31 changes: 17 additions & 14 deletions lib/utils/map_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,32 @@ extension AnimationUtils on MapController {
Duration duration = const Duration(milliseconds: 500),
String? id,
}) {
location ??= camera.center;
zoom ??= camera.zoom;
rotation ??= camera.rotation;

final positionTween = LatLngTween(begin: camera.center, end: location);
final zoomTween = Tween<double>(begin: camera.zoom, end: zoom);
final rotationTween = RotationTween(begin: camera.rotation, end: rotation);
final positionTween = location != null
? LatLngTween(begin: camera.center, end: location)
: null;
final zoomTween = zoom != null
? Tween<double>(begin: camera.zoom, end: zoom)
: null;
final rotationTween = rotation != null
? RotationTween(begin: camera.rotation, end: rotation)
: null;

final controller = AnimationController(
duration: duration,
vsync: ticker
vsync: ticker,
);
final animation = CurvedAnimation(
parent: controller,
curve: curve
parent: controller,
curve: curve,
);

animation.addListener(() {
moveAndRotate(
positionTween.evaluate(animation),
zoomTween.evaluate(animation),
rotationTween.evaluate(animation),
id: id
// use most up to date camera value if no tween/animation was specified
positionTween?.evaluate(animation) ?? camera.center,
zoomTween?.evaluate(animation) ?? camera.zoom,
rotationTween?.evaluate(animation) ?? camera.rotation,
id: id,
);
});

Expand Down
Loading

0 comments on commit a026085

Please sign in to comment.