mapbox / mapbox/mapbox-maps-flutter

Add Interpolator and AnimatorListener to MapAnimationOptions

Open
#731 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Dart
Stars
380
Forks
204
PR merge metrics
No merged PRs in 30d

Description

I am trying to recreate this rotating globe animation from the [Android](https://docs.mapbox.com/android/maps/examples/create-a-rotating-globe/) and [iOS](https://docs.mapbox.com/ios/maps/examples/rotating-globe/) SDK in Flutter, but it seems like there are some missing parameters in the `MapAnimationOptions` class, notably `Interpolator` for indicating the animation curve, and `AnimatorListener` for listening to the animation onEnd event.

My current implementation has jittery animation due to the animation curve not being linear. I'm wondering if there is an easier way to do this without the missing animation parameters?

```dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:heritage_quest/util/env.dart';
import 'package:mapbox_maps_flutter/mapbox_maps_flutter.dart';

class MapPage extends StatefulWidget {
const MapPage({super.key});

@override
State createState() => MapPageState();
}

class MapPageState extends State {
late MapboxMap _map;
late Timer _timer;
bool _spinEnabled = true;
bool _userInteracting = false;
final double _secondsPerRevolution = 120;
final double _maxSpinZoom = 5;
final double _slowSpinZoom = 3;

@override
void initState() {
super.initState();
MapboxOptions.setAccessToken(Env.mapboxToken);
}

void _onMapCreated(MapboxMap mapboxMap) {
_map = mapboxMap;
_map.style.setProjection(StyleProjection(name: StyleProjectionName.globe));
_startSpinning();
}

void _startSpinning() {
_timer = Timer.periodic(Duration(milliseconds: 1000), (timer) {
_spinGlobe();
});
}

Future _spinGlobe() async {
if (_spinEnabled && !_userInteracting) {
final position = await _map.getCameraState();
double zoom = position.zoom;
if (zoom < _maxSpinZoom) {
double distancePerSecond = 360 / _secondsPerRevolution;
if (zoom > _slowSpinZoom) {
double zoomDif = (_maxSpinZoom - zoom) / (_maxSpinZoom - _slowSpinZoom);
distancePerSecond *= zoomDif;
}
double newLng = position.center.coordinates.lng - distancePerSecond;
_map.easeTo(
CameraOptions(
center: Point(
coordinates: Position(newLng, position.center.coordinates.lat),
),
),
MapAnimationOptions(duration: 1000),
);
}
}
}

void _onScrollListener(MapContentGestureContext gesture) {
_timer.cancel();
print(gesture.touchPosition.toString());
}

void _onMapIdleListener(MapIdleEventData event) {
print("I'm idle");
_map.getCameraState().then((value) {
if (canSpin(value.zoom)) {
_startSpinning();
}
});
}

bool canSpin(double zoom) => _spinEnabled && !_userInteracting && zoom < _maxSpinZoom;

@override
void dispose() {
_timer.cancel();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
MapWidget(
cameraOptions: CameraOptions(zoom: 3),
onMapIdleListener: _onMapIdleListener,
onScrollListener: _onScrollListener,
key: const ValueKey("mapWidget"),
styleUri: MapboxStyles.STANDARD_SATELLITE,
onMapCreated: _onMapCreated,
),
],
),
);
}
}

```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at MapAnimationOptions and the easeTo call shown in the issue, then trace how animation options cross the Flutter and native boundary. Done means the options support an animation interpolator and an end-listener equivalent for the rotating-globe use case without requiring the workaround shown.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, flutter
Domain
api, mobile-dev
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.