-
-
Notifications
You must be signed in to change notification settings - Fork 895
Expand file tree
/
Copy pathmap_controller_impl.dart
More file actions
796 lines (705 loc) · 21.3 KB
/
map_controller_impl.dart
File metadata and controls
796 lines (705 loc) · 21.3 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/gestures/map_interactive_viewer.dart';
import 'package:flutter_map/src/misc/deg_rad_conversions.dart';
import 'package:flutter_map/src/misc/extensions.dart';
import 'package:flutter_map/src/misc/move_and_rotate_result.dart';
import 'package:latlong2/latlong.dart';
/// Implements [MapController] whilst exposing methods for internal use which
/// should not be visible to the user (e.g. for setting the current camera).
/// This controller is for internal use. All updates to the state should be done
/// by calling methods of this class to ensure consistency.
class MapControllerImpl extends ValueNotifier<_MapControllerState>
implements MapController {
final _mapEventStreamController = StreamController<MapEvent>.broadcast();
late MapInteractiveViewerState _interactiveViewerState;
Animation<LatLng>? _moveAnimation;
Animation<double>? _zoomAnimation;
Animation<double>? _rotationAnimation;
Animation<Offset>? _flingAnimation;
late bool _animationHasGesture;
late Offset _animationOffset;
late Offset _flingMapCenterStartPoint;
/// Constructor of the [MapController] implementation for internal usage.
MapControllerImpl({MapOptions? options, TickerProvider? vsync})
: super(
_MapControllerState(
options: options,
camera: options == null ? null : MapCamera.initialCamera(options),
animationController:
vsync == null ? null : AnimationController(vsync: vsync),
),
) {
value.animationController?.addListener(_handleAnimation);
}
/// Link the viewer state with the controller. This should be done once when
/// the FlutterMapInteractiveViewerState is initialized.
set interactiveViewerState(
MapInteractiveViewerState interactiveViewerState,
) =>
_interactiveViewerState = interactiveViewerState;
StreamSink<MapEvent> get _mapEventSink => _mapEventStreamController.sink;
@override
Stream<MapEvent> get mapEventStream => _mapEventStreamController.stream;
/// Used to change [MapOptions] and update the required widgets.
MapOptions get options {
return value.options ??
(throw Exception('You need to have the FlutterMap widget rendered at '
'least once before using the MapController.'));
}
/// Get the current [MapCamera] instance. Prefer using
/// `MapCamera.of(context)` if possible.
@override
MapCamera get camera {
return value.camera ??
(throw Exception('You need to have the FlutterMap widget rendered at '
'least once before using the MapController.'));
}
AnimationController get _animationController {
return value.animationController ??
(throw Exception('You need to have the FlutterMap widget rendered at '
'least once before using the MapController.'));
}
/// This setter should only be called in this class or within tests. Changes
/// to the [_MapControllerState] should be done via methods in this class.
@visibleForTesting
@override
// ignore: library_private_types_in_public_api
set value(_MapControllerState value) => super.value = value;
@override
bool move(
LatLng center,
double zoom, {
Offset offset = Offset.zero,
String? id,
}) =>
moveRaw(
center,
zoom,
offset: offset,
hasGesture: false,
source: MapEventSource.mapController,
id: id,
);
@override
bool rotate(double degree, {String? id}) => rotateRaw(
degree,
hasGesture: false,
source: MapEventSource.mapController,
id: id,
);
@override
MoveAndRotateResult rotateAroundPoint(
double degree, {
Offset? offset,
String? id,
}) =>
rotateAroundPointRaw(
degree,
offset: offset,
hasGesture: false,
source: MapEventSource.mapController,
id: id,
);
@override
MoveAndRotateResult moveAndRotate(
LatLng center,
double zoom,
double degree, {
String? id,
}) =>
moveAndRotateRaw(
center,
zoom,
degree,
offset: Offset.zero,
hasGesture: false,
source: MapEventSource.mapController,
id: id,
);
@override
bool fitCamera(CameraFit cameraFit) => fitCameraRaw(cameraFit);
/// Internal endpoint to move the [MapCamera] and change zoom level.
bool moveRaw(
LatLng newCenter,
double newZoom, {
Offset offset = Offset.zero,
required bool hasGesture,
required MapEventSource source,
String? id,
}) {
// Algorithm thanks to https://github.com/tlserver/flutter_map_location_marker
LatLng center = newCenter;
if (offset != Offset.zero) {
final newPoint = camera.projectAtZoom(newCenter, newZoom);
center = camera.unprojectAtZoom(
camera.rotatePoint(
newPoint,
newPoint - offset,
),
newZoom,
);
}
MapCamera? newCamera = camera.withPosition(
center: center,
zoom: camera.clampZoom(newZoom),
);
newCamera = options.cameraConstraint.constrain(newCamera);
if (newCamera == null ||
(newCamera.center == camera.center && newCamera.zoom == camera.zoom)) {
return false;
}
if (newCamera.constrained) {
if (options.cameraConstraint is ContainCamera) {
final ContainCamera containCamera =
options.cameraConstraint as ContainCamera;
if (containCamera.bounds.west == -180 &&
containCamera.bounds.east == 180) {
return false;
}
}
}
final oldCamera = camera;
value = value.withMapCamera(newCamera);
final movementEvent = MapEventWithMove.fromSource(
oldCamera: oldCamera,
camera: camera,
hasGesture: hasGesture,
source: source,
id: id,
);
//! PREVENTS TILE LOADING IF SOURCE NOT CONFIGURED IN CONSTRUCTOR
if (movementEvent != null) _emitMapEvent(movementEvent);
options.onPositionChanged?.call(newCamera, hasGesture);
return true;
}
/// Internal endpoint to rotate the [MapCamera].
bool rotateRaw(
double newRotation, {
required bool hasGesture,
required MapEventSource source,
String? id,
}) {
if (newRotation == camera.rotation) return false;
final newCamera = options.cameraConstraint.constrain(
camera.withRotation(newRotation),
);
if (newCamera == null) return false;
final oldCamera = camera;
// Update camera then emit events and callbacks
value = value.withMapCamera(newCamera);
_emitMapEvent(
MapEventRotate(
id: id,
source: source,
oldCamera: oldCamera,
camera: camera,
),
);
return true;
}
/// Internal endpoint to rotate around a point that is not in the center of
/// the map.
MoveAndRotateResult rotateAroundPointRaw(
double degree, {
required Offset? offset,
required bool hasGesture,
required MapEventSource source,
String? id,
}) {
if (degree == camera.rotation) {
return const (moveSuccess: false, rotateSuccess: false);
}
if (offset == Offset.zero) {
return (
moveSuccess: true,
rotateSuccess: rotateRaw(
degree,
hasGesture: hasGesture,
source: source,
id: id,
),
);
}
final rotationDiff = degree - camera.rotation;
final rotationCenter = camera.projectAtZoom(camera.center) +
offset!.rotate(camera.rotationRad);
return (
moveSuccess: moveRaw(
camera.unprojectAtZoom(
rotationCenter +
(camera.projectAtZoom(camera.center) - rotationCenter)
.rotate(degrees2Radians * rotationDiff),
),
camera.zoom,
hasGesture: hasGesture,
source: source,
id: id,
),
rotateSuccess: rotateRaw(
camera.rotation + rotationDiff,
hasGesture: hasGesture,
source: source,
id: id,
),
);
}
/// Internal endpoint to move, rotate and change zoom level
/// of the [MapCamera].
MoveAndRotateResult moveAndRotateRaw(
LatLng newCenter,
double newZoom,
double newRotation, {
required Offset offset,
required bool hasGesture,
required MapEventSource source,
String? id,
}) =>
(
moveSuccess: moveRaw(
newCenter,
newZoom,
offset: offset,
hasGesture: hasGesture,
source: source,
id: id,
),
rotateSuccess: rotateRaw(
newRotation,
id: id,
source: source,
hasGesture: hasGesture,
),
);
///
bool fitCameraRaw(
CameraFit cameraFit, {
Offset offset = Offset.zero,
}) {
final fitted = cameraFit.fit(camera);
return moveRaw(
fitted.center,
fitted.zoom,
offset: offset,
hasGesture: false,
source: MapEventSource.mapController,
);
}
/// Set the widget size but don't emit a event to the event system.
bool setNonRotatedSizeWithoutEmittingEvent(
Size nonRotatedSize,
) {
if (nonRotatedSize != MapCamera.kImpossibleSize &&
nonRotatedSize != camera.nonRotatedSize) {
value = value.withMapCamera(camera.withNonRotatedSize(nonRotatedSize));
return true;
}
return false;
}
set options(MapOptions newOptions) {
final newCamera = value.camera?.withOptions(newOptions) ??
MapCamera.initialCamera(newOptions);
assert(
newOptions.cameraConstraint.constrain(newCamera) == newCamera,
'MapCamera is no longer within the cameraConstraint after an option change.',
);
if (value.options != null &&
value.options!.interactionOptions != newOptions.interactionOptions) {
_interactiveViewerState.updateGestures(
value.options!.interactionOptions,
newOptions.interactionOptions,
);
}
value = _MapControllerState(
options: newOptions,
camera: newCamera,
animationController: value.animationController,
);
}
set vsync(TickerProvider tickerProvider) {
if (value.animationController == null) {
value = _MapControllerState(
options: value.options,
camera: value.camera,
animationController: AnimationController(vsync: tickerProvider)
..addListener(_handleAnimation),
);
} else {
_animationController.resync(tickerProvider);
}
}
/// To be called when a gesture that causes movement starts.
void moveStarted(MapEventSource source) {
_emitMapEvent(
MapEventMoveStart(
camera: camera,
source: source,
),
);
}
/// To be called when an ongoing drag movement updates.
void dragUpdated(MapEventSource source, Offset offset) {
final oldCenterPt = camera.projectAtZoom(camera.center);
final newCenterPt = oldCenterPt + offset;
final newCenter = camera.unprojectAtZoom(newCenterPt);
moveRaw(
newCenter,
camera.zoom,
hasGesture: true,
source: source,
);
}
/// To be called when a drag gesture ends.
void moveEnded(MapEventSource source) {
_emitMapEvent(
MapEventMoveEnd(
camera: camera,
source: source,
),
);
}
/// To be called when a rotation gesture starts.
void rotateStarted(MapEventSource source) {
_emitMapEvent(
MapEventRotateStart(
camera: camera,
source: source,
),
);
}
/// To be called when a rotation gesture ends.
void rotateEnded(MapEventSource source) {
_emitMapEvent(
MapEventRotateEnd(
camera: camera,
source: source,
),
);
}
/// To be called when a fling gesture starts.
void flingStarted(MapEventSource source) {
_emitMapEvent(
MapEventFlingAnimationStart(
camera: camera,
source: MapEventSource.flingAnimationController,
),
);
}
/// To be called when a fling gesture ends.
void flingEnded(MapEventSource source) {
_emitMapEvent(
MapEventFlingAnimationEnd(
camera: camera,
source: source,
),
);
}
/// To be called when a fling gesture does not start.
void flingNotStarted(MapEventSource source) {
_emitMapEvent(
MapEventFlingAnimationNotStarted(
camera: camera,
source: source,
),
);
}
/// To be called when a double tap zoom starts.
void doubleTapZoomStarted(MapEventSource source) {
_emitMapEvent(
MapEventDoubleTapZoomStart(
camera: camera,
source: source,
),
);
}
/// To be called when a double tap zoom ends.
void doubleTapZoomEnded(MapEventSource source) {
_emitMapEvent(
MapEventDoubleTapZoomEnd(
camera: camera,
source: source,
),
);
}
/// Called when a long-press gesture has happened, calls the
/// [MapOptions.onTap] callback and emits a [MapEventTap] event.
void tapped(
MapEventSource source,
TapPosition tapPosition,
LatLng position,
) {
options.onTap?.call(tapPosition, position);
_emitMapEvent(
MapEventTap(
tapPosition: position,
camera: camera,
source: source,
),
);
}
/// Called when a long-press gesture has happened, calls the
/// [MapOptions.onSecondaryTap] callback and emits a
/// [MapEventSecondaryTap] event.
void secondaryTapped(
MapEventSource source,
TapPosition tapPosition,
LatLng position,
) {
options.onSecondaryTap?.call(tapPosition, position);
_emitMapEvent(
MapEventSecondaryTap(
tapPosition: position,
camera: camera,
source: source,
),
);
}
/// Called when a long-press gesture has happened, calls the
/// [MapOptions.onLongPress] callback and emits a [MapEventLongPress] event.
void longPressed(
MapEventSource source,
TapPosition tapPosition,
LatLng position,
) {
options.onLongPress?.call(tapPosition, position);
_emitMapEvent(
MapEventLongPress(
tapPosition: position,
camera: camera,
source: MapEventSource.longPress,
),
);
}
/// To be called when the map's size constraints change.
void nonRotatedSizeChange(
MapEventSource source,
MapCamera oldCamera,
MapCamera newCamera,
) {
_emitMapEvent(
MapEventNonRotatedSizeChange(
source: MapEventSource.nonRotatedSizeChange,
oldCamera: oldCamera,
camera: newCamera,
),
);
}
/// Move and rotate the the map with an animation.
/// The raw method allows to set all parameters.
void moveAndRotateAnimatedRaw(
LatLng newCenter,
double newZoom,
double newRotation, {
required Offset offset,
required Duration duration,
required Curve curve,
required bool hasGesture,
required MapEventSource source,
}) {
if (newRotation == camera.rotation) {
moveAnimatedRaw(
newCenter,
newZoom,
duration: duration,
curve: curve,
hasGesture: hasGesture,
source: source,
);
return;
}
// cancel all ongoing animation
_animationController.stop();
_resetAnimations();
if (newCenter == camera.center && newZoom == camera.zoom) return;
// create the new animation
_moveAnimation = LatLngTween(begin: camera.center, end: newCenter)
.chain(CurveTween(curve: curve))
.animate(_animationController);
_zoomAnimation = Tween<double>(begin: camera.zoom, end: newZoom)
.chain(CurveTween(curve: curve))
.animate(_animationController);
_rotationAnimation = Tween<double>(begin: camera.rotation, end: newRotation)
.chain(CurveTween(curve: curve))
.animate(_animationController);
_animationController.duration = duration;
_animationHasGesture = hasGesture;
_animationOffset = offset;
// start the animation from its start
_animationController.forward(from: 0);
}
/// Animated rotation of the map.
/// The raw method allows to set all parameters.
void rotateAnimatedRaw(
double newRotation, {
required Offset offset,
required Duration duration,
required Curve curve,
required bool hasGesture,
required MapEventSource source,
}) {
// cancel all ongoing animation
_animationController.stop();
_resetAnimations();
if (newRotation == camera.rotation) return;
// create the new animation
_rotationAnimation = Tween<double>(begin: camera.rotation, end: newRotation)
.chain(CurveTween(curve: curve))
.animate(_animationController);
_animationController.duration = duration;
_animationHasGesture = hasGesture;
_animationOffset = offset;
// start the animation from its start
_animationController.forward(from: 0);
}
/// Stops all ongoing animations of the [MapControllerImpl].
/// This is commonly used by other gestures that should stop all
/// ongoing movement.
void stopAnimationRaw({bool canceled = true}) {
if (isAnimating) _animationController.stop(canceled: canceled);
}
/// Getter that returns true if the [MapControllerImpl] performs a zoom,
/// drag or rotate animation.
bool get isAnimating => _animationController.isAnimating;
void _resetAnimations() {
_moveAnimation = null;
_rotationAnimation = null;
_zoomAnimation = null;
_flingAnimation = null;
}
/// Fling animation for the map.
/// The raw method allows to set all parameters.
void flingAnimatedRaw({
required double velocity,
required Offset direction,
required Offset begin,
Offset offset = Offset.zero,
double mass = 1,
double stiffness = 1000,
double ratio = 5,
required bool hasGesture,
}) {
// cancel all ongoing animation
_animationController.stop();
_resetAnimations();
_animationHasGesture = hasGesture;
_animationOffset = offset;
_flingMapCenterStartPoint = camera.projectAtZoom(camera.center);
final distance = (Offset.zero & camera.nonRotatedSize).shortestSide;
_flingAnimation = Tween<Offset>(
begin: begin,
end: begin - direction * distance,
).animate(_animationController);
_animationController.value = 0;
_animationController.fling(
velocity: velocity,
springDescription: SpringDescription.withDampingRatio(
mass: mass,
stiffness: stiffness,
ratio: ratio,
),
);
}
/// Animated movement of the map.
/// The raw method allows to set all parameters.
void moveAnimatedRaw(
LatLng newCenter,
double newZoom, {
Offset offset = Offset.zero,
required Duration duration,
required Curve curve,
required bool hasGesture,
required MapEventSource source,
}) {
// cancel all ongoing animation
_animationController.stop();
_resetAnimations();
if (newCenter == camera.center && newZoom == camera.zoom) return;
// create the new animation
_moveAnimation = LatLngTween(begin: camera.center, end: newCenter)
.chain(CurveTween(curve: curve))
.animate(_animationController);
_zoomAnimation = Tween<double>(begin: camera.zoom, end: newZoom)
.chain(CurveTween(curve: curve))
.animate(_animationController);
_animationController.duration = duration;
_animationHasGesture = hasGesture;
_animationOffset = offset;
// start the animation from its start
_animationController.forward(from: 0);
}
void _emitMapEvent(MapEvent event) {
if (event.source == MapEventSource.mapController && event is MapEventMove) {
_interactiveViewerState.interruptAnimatedMovement(event);
}
options.onMapEvent?.call(event);
_mapEventSink.add(event);
}
void _handleAnimation() {
// fling animation
if (_flingAnimation != null) {
final newCenterPoint = _flingMapCenterStartPoint +
_flingAnimation!.value.rotate(camera.rotationRad);
moveRaw(
camera.unprojectAtZoom(newCenterPoint),
camera.zoom,
hasGesture: _animationHasGesture,
source: MapEventSource.flingAnimationController,
offset: _animationOffset,
);
return;
}
// animated movement
if (_moveAnimation != null) {
if (_rotationAnimation != null) {
moveAndRotateRaw(
_moveAnimation?.value ?? camera.center,
_zoomAnimation?.value ?? camera.zoom,
_rotationAnimation!.value,
hasGesture: _animationHasGesture,
source: MapEventSource.mapController,
offset: _animationOffset,
);
} else {
moveRaw(
_moveAnimation!.value,
_zoomAnimation?.value ?? camera.zoom,
hasGesture: _animationHasGesture,
source: MapEventSource.mapController,
offset: _animationOffset,
);
}
return;
}
// animated rotation
if (_rotationAnimation != null) {
rotateRaw(
_rotationAnimation!.value,
hasGesture: _animationHasGesture,
source: MapEventSource.mapController,
);
}
}
@override
void dispose() {
_mapEventStreamController.close();
value.animationController?.dispose();
super.dispose();
}
}
@immutable
class _MapControllerState {
final MapCamera? camera;
final MapOptions? options;
final AnimationController? animationController;
const _MapControllerState({
required this.options,
required this.camera,
required this.animationController,
});
_MapControllerState withMapCamera(MapCamera camera) => _MapControllerState(
options: options,
camera: camera,
animationController: animationController,
);
}