-
-
Notifications
You must be signed in to change notification settings - Fork 860
/
animated_map_controller.dart
233 lines (213 loc) · 8.27 KB
/
animated_map_controller.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_cancellable_tile_provider/flutter_map_cancellable_tile_provider.dart';
import 'package:flutter_map_example/widgets/drawer/menu_drawer.dart';
import 'package:latlong2/latlong.dart';
class AnimatedMapControllerPage extends StatefulWidget {
static const String route = '/map_controller_animated';
const AnimatedMapControllerPage({super.key});
@override
AnimatedMapControllerPageState createState() =>
AnimatedMapControllerPageState();
}
class AnimatedMapControllerPageState extends State<AnimatedMapControllerPage>
with TickerProviderStateMixin {
static const _startedId = 'AnimatedMapController#MoveStarted';
static const _inProgressId = 'AnimatedMapController#MoveInProgress';
static const _finishedId = 'AnimatedMapController#MoveFinished';
static const _london = LatLng(51.5, -0.09);
static const _paris = LatLng(48.8566, 2.3522);
static const _dublin = LatLng(53.3498, -6.2603);
static const _markers = [
Marker(
width: 80,
height: 80,
point: _london,
child: FlutterLogo(key: ValueKey('blue')),
),
Marker(
width: 80,
height: 80,
point: _dublin,
child: FlutterLogo(key: ValueKey('green')),
),
Marker(
width: 80,
height: 80,
point: _paris,
child: FlutterLogo(key: ValueKey('purple')),
),
];
final mapController = MapController();
void _animatedMapMove(LatLng destLocation, double destZoom) {
// Create some tweens. These serve to split up the transition from one location to another.
// In our case, we want to split the transition be<tween> our current map center and the destination.
final camera = mapController.camera;
final latTween = Tween<double>(
begin: camera.center.latitude, end: destLocation.latitude);
final lngTween = Tween<double>(
begin: camera.center.longitude, end: destLocation.longitude);
final zoomTween = Tween<double>(begin: camera.zoom, end: destZoom);
// Create a animation controller that has a duration and a TickerProvider.
final controller = AnimationController(
duration: const Duration(milliseconds: 500), vsync: this);
// The animation determines what path the animation will take. You can try different Curves values, although I found
// fastOutSlowIn to be my favorite.
final Animation<double> animation =
CurvedAnimation(parent: controller, curve: Curves.fastOutSlowIn);
// Note this method of encoding the target destination is a workaround.
// When proper animated movement is supported (see #1263) we should be able
// to detect an appropriate animated movement event which contains the
// target zoom/center.
final startIdWithTarget =
'$_startedId#${destLocation.latitude},${destLocation.longitude},$destZoom';
bool hasTriggeredMove = false;
controller.addListener(() {
final String id;
if (animation.value == 1.0) {
id = _finishedId;
} else if (!hasTriggeredMove) {
id = startIdWithTarget;
} else {
id = _inProgressId;
}
hasTriggeredMove |= mapController.move(
LatLng(latTween.evaluate(animation), lngTween.evaluate(animation)),
zoomTween.evaluate(animation),
id: id,
);
});
animation.addStatusListener((status) {
if (status == AnimationStatus.completed) {
controller.dispose();
} else if (status == AnimationStatus.dismissed) {
controller.dispose();
}
});
controller.forward();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Animated MapController')),
drawer: const MenuDrawer(AnimatedMapControllerPage.route),
body: Padding(
padding: const EdgeInsets.all(8),
child: Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Row(
children: <Widget>[
MaterialButton(
onPressed: () => _animatedMapMove(_london, 10),
child: const Text('London'),
),
MaterialButton(
onPressed: () => _animatedMapMove(_paris, 5),
child: const Text('Paris'),
),
MaterialButton(
onPressed: () => _animatedMapMove(_dublin, 5),
child: const Text('Dublin'),
),
],
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Row(
children: <Widget>[
MaterialButton(
onPressed: () {
final bounds = LatLngBounds.fromPoints([
_dublin,
_paris,
_london,
]);
mapController.fitCamera(
CameraFit.bounds(
bounds: bounds,
padding: const EdgeInsets.symmetric(horizontal: 15),
),
);
},
child: const Text('Fit Bounds'),
),
MaterialButton(
onPressed: () {
final bounds = LatLngBounds.fromPoints([
_dublin,
_paris,
_london,
]);
final constrained = CameraFit.bounds(
bounds: bounds,
).fit(mapController.camera);
_animatedMapMove(constrained.center, constrained.zoom);
},
child: const Text('Fit Bounds animated'),
),
],
),
),
Flexible(
child: FlutterMap(
mapController: mapController,
options: const MapOptions(
initialCenter: LatLng(51.5, -0.09),
initialZoom: 5,
maxZoom: 10,
minZoom: 3,
),
children: [
TileLayer(
urlTemplate:
'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
userAgentPackageName: 'dev.fleaflet.flutter_map.example',
tileProvider: CancellableNetworkTileProvider(),
tileUpdateTransformer: _animatedMoveTileUpdateTransformer,
),
const MarkerLayer(markers: _markers),
],
),
),
],
),
),
);
}
}
/// Causes tiles to be prefetched at the target location and disables pruning
/// whilst animating movement. When proper animated movement is added (see
/// #1263) we should just detect the appropriate AnimatedMove events and
/// use their target zoom/center.
final _animatedMoveTileUpdateTransformer =
TileUpdateTransformer.fromHandlers(handleData: (updateEvent, sink) {
final mapEvent = updateEvent.mapEvent;
final id = mapEvent is MapEventMove ? mapEvent.id : null;
if (id?.startsWith(AnimatedMapControllerPageState._startedId) ?? false) {
final parts = id!.split('#')[2].split(',');
final lat = double.parse(parts[0]);
final lon = double.parse(parts[1]);
final zoom = double.parse(parts[2]);
// When animated movement starts load tiles at the target location and do
// not prune. Disabling pruning means existing tiles will remain visible
// whilst animating.
sink.add(
updateEvent.loadOnly(
loadCenterOverride: LatLng(lat, lon),
loadZoomOverride: zoom,
),
);
} else if (id == AnimatedMapControllerPageState._inProgressId) {
// Do not prune or load whilst animating so that any existing tiles remain
// visible. A smarter implementation may start pruning once we are close to
// the target zoom/location.
} else if (id == AnimatedMapControllerPageState._finishedId) {
// We already prefetched the tiles when animation started so just prune.
sink.add(updateEvent.pruneOnly());
} else {
sink.add(updateEvent);
}
});