Skip to content

Commit

Permalink
Add duration for animateCamera (#1066)
Browse files Browse the repository at this point in the history
  • Loading branch information
morvagergely authored May 31, 2022
1 parent b1479f4 commit 3496907
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions example/lib/animate_camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class AnimateCameraState extends State<AnimateCamera> {
CameraUpdate.newLatLng(
const LatLng(56.1725505, 10.1850512),
),
duration: Duration(seconds: 5),
)
.then((result) => print(
"mapController.animateCamera() returned $result"));
Expand Down
3 changes: 2 additions & 1 deletion ios/Classes/MapboxMapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,9 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
animationTimingFunction: CAMediaTimingFunction(name: CAMediaTimingFunctionName
.easeInEaseOut))
result(nil)
} else {
mapView.setCamera(camera, animated: true)
}
mapView.setCamera(camera, animated: true)
}
result(nil)

Expand Down
7 changes: 5 additions & 2 deletions lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,15 @@ class MapboxMapController extends ChangeNotifier {

/// Starts an animated change of the map camera position.
///
/// [duration] is the amount of time, that the transition animation should take.
///
/// The returned [Future] completes after the change has been started on the
/// platform side.
/// It returns true if the camera was successfully moved and false if the movement was canceled.
/// Note: this currently always returns immediately with a value of null on iOS
Future<bool?> animateCamera(CameraUpdate cameraUpdate) async {
return _mapboxGlPlatform.animateCamera(cameraUpdate);
Future<bool?> animateCamera(CameraUpdate cameraUpdate,
{Duration? duration}) async {
return _mapboxGlPlatform.animateCamera(cameraUpdate, duration: duration);
}

/// Instantaneously re-position the camera.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ abstract class MapboxGlPlatform {
OnPlatformViewCreatedCallback onPlatformViewCreated,
Set<Factory<OneSequenceGestureRecognizer>>? gestureRecognizers);
Future<CameraPosition?> updateMapOptions(Map<String, dynamic> optionsUpdate);
Future<bool?> animateCamera(CameraUpdate cameraUpdate);
Future<bool?> animateCamera(CameraUpdate cameraUpdate, {Duration? duration});
Future<bool?> moveCamera(CameraUpdate cameraUpdate);
Future<void> updateMyLocationTrackingMode(
MyLocationTrackingMode myLocationTrackingMode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,10 @@ class MethodChannelMapboxGl extends MapboxGlPlatform {
}

@override
Future<bool?> animateCamera(cameraUpdate) async {
Future<bool?> animateCamera(cameraUpdate, {Duration? duration}) async {
return await _channel.invokeMethod('camera#animate', <String, dynamic>{
'cameraUpdate': cameraUpdate.toJson(),
'duration': duration?.inMilliseconds,
});
}

Expand Down
1 change: 1 addition & 0 deletions mapbox_gl_web/lib/mapbox_gl_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:async';
import 'dart:html';
// ignore: unused_import
import 'dart:js';
import 'dart:js_util';
import 'dart:math';
import 'dart:typed_data';
import 'dart:ui' as ui;
Expand Down
20 changes: 18 additions & 2 deletions mapbox_gl_web/lib/src/mapbox_web_gl_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,25 @@ class MapboxWebGlPlatform extends MapboxGlPlatform
}

@override
Future<bool?> animateCamera(CameraUpdate cameraUpdate) async {
Future<bool?> animateCamera(CameraUpdate cameraUpdate,
{Duration? duration}) async {
final cameraOptions = Convert.toCameraOptions(cameraUpdate, _map);
_map.flyTo(cameraOptions);

final around = getProperty(cameraOptions, 'around');
final bearing = getProperty(cameraOptions, 'bearing');
final center = getProperty(cameraOptions, 'center');
final pitch = getProperty(cameraOptions, 'pitch');
final zoom = getProperty(cameraOptions, 'zoom');

_map.flyTo({
if (around.jsObject != null) 'around': around,
if (bearing != null) 'bearing': bearing,
if (center.jsObject != null) 'center': center,
if (pitch != null) 'pitch': pitch,
if (zoom != null) 'zoom': zoom,
if (duration != null) 'duration': duration.inMilliseconds,
});

return true;
}

Expand Down

0 comments on commit 3496907

Please sign in to comment.