Skip to content

Commit 979948d

Browse files
authored
feat: Add Camera.EaseTo function (#17)
1 parent 1838c7a commit 979948d

File tree

6 files changed

+91
-19
lines changed

6 files changed

+91
-19
lines changed

src/libs/Mapbox.Maui/IMapboxView.cs

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public interface IMapboxController
7070
public interface IMapCameraController
7171
{
7272
void FlyTo(CameraOptions cameraOptions, AnimationOptions animationOptions = default, Action<AnimationState> completion = default);
73+
void EaseTo(CameraOptions cameraOptions, AnimationOptions animationOptions = default, Action<AnimationState> completion = default);
7374
}
7475

7576
public class MapTappedEventArgs : EventArgs

src/libs/Mapbox.Maui/Platforms/Android/MapboxViewHandler.Camera.cs

+19
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@ namespace MapboxMaui;
55

66
partial class MapboxViewHandler : IMapCameraController
77
{
8+
public void EaseTo(CameraOptions cameraOptions, AnimationOptions animationOptions = null, Action<AnimationState> completion = null)
9+
{
10+
var mapView = mapboxFragment?.MapView;
11+
12+
if (mapView == null) return;
13+
14+
var xcameraOptions = cameraOptions.ToNative();
15+
var xanimationOptions = animationOptions?.ToNative();
16+
17+
// TODO Return Cancellable obj
18+
CameraAnimationsUtils.EaseTo(
19+
mapView.MapboxMap,
20+
xcameraOptions,
21+
xanimationOptions,
22+
completion != null
23+
? new XAnimationListener(completion)
24+
: null);
25+
}
26+
827
public void FlyTo(CameraOptions cameraOptions, AnimationOptions animationOptions = default, Action<AnimationState> completion = default)
928
{
1029
var mapView = mapboxFragment?.MapView;

src/libs/Mapbox.Maui/Platforms/iOS/MapboxViewHandler.Camera.cs

+23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@ namespace MapboxMaui;
44

55
partial class MapboxViewHandler : IMapCameraController
66
{
7+
public void EaseTo(CameraOptions cameraOptions, AnimationOptions animationOptions = default, Action<AnimationState> completion = default)
8+
{
9+
var mapView = PlatformView.MapView;
10+
11+
if (mapView == null) return;
12+
13+
var xcameraOptions = cameraOptions.ToNative();
14+
mapView.Camera().FlyTo(
15+
xcameraOptions,
16+
animationOptions?.Duration / 1000L ?? 0,
17+
(position) =>
18+
{
19+
var xposition = position switch
20+
{
21+
UIKit.UIViewAnimatingPosition.Start => AnimatingPosition.Start,
22+
UIKit.UIViewAnimatingPosition.End => AnimatingPosition.End,
23+
UIKit.UIViewAnimatingPosition.Current => AnimatingPosition.Current,
24+
_ => AnimatingPosition.Current,
25+
};
26+
27+
completion?.Invoke(new AnimationState(xposition));
28+
});
29+
}
730
public void FlyTo(CameraOptions cameraOptions, AnimationOptions animationOptions = default, Action<AnimationState> completion = default)
831
{
932
var mapView = PlatformView.MapView;

src/qs/MapboxMauiQs/Examples/Lab/65.CameraFlyAnimation/CameraFlyAnimationExample.cs src/qs/MapboxMauiQs/Examples/Lab/65.CameraAnimations/CameraAnimationsExample.cs

+37-8
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,43 @@
11
namespace MapboxMauiQs;
22

3-
public class CameraFlyAnimationExample : ContentPage, IExamplePage, IQueryAttributable
3+
public class CameraAnimationsExample : ContentPage, IExamplePage, IQueryAttributable
44
{
55
MapboxView map;
66
IExampleInfo info;
77

8-
public CameraFlyAnimationExample()
8+
public CameraAnimationsExample()
99
{
1010
iOSPage.SetUseSafeArea(this, false);
1111

1212
var grid = new Grid();
1313
map = new MapboxView();
1414
grid.Children.Add(map);
1515

16-
var btnMoveCamera = new Button()
16+
var btnFlyCamera = new Button()
1717
{
18-
Text = "Move Camera",
18+
Text = "Fly to Hanoi",
19+
};
20+
btnFlyCamera.Clicked += HandleCameraFlyTo;
21+
22+
var btnEaseCamera = new Button()
23+
{
24+
Text = "Ease to HCMC",
25+
};
26+
btnEaseCamera.Clicked += HandleCameraEaseTo;
27+
28+
var btns = new HorizontalStackLayout
29+
{
30+
Children = {
31+
btnFlyCamera,
32+
btnEaseCamera,
33+
},
1934
VerticalOptions = LayoutOptions.End,
2035
HorizontalOptions = LayoutOptions.Center,
2136
Margin = new Thickness(24),
37+
Spacing = 16,
2238
};
23-
btnMoveCamera.Clicked += HandleMoveCamera;
24-
grid.Children.Add(btnMoveCamera);
39+
40+
grid.Children.Add(btns);
2541

2642
map.MapReady += Map_MapReady;
2743
map.StyleLoaded += Map_StyleLoaded;
@@ -30,9 +46,9 @@ public CameraFlyAnimationExample()
3046
Content = grid;
3147
}
3248

33-
private void HandleMoveCamera(object sender, EventArgs e)
49+
private void HandleCameraFlyTo(object sender, EventArgs e)
3450
{
35-
var centerLocation = new MapPosition(21.0278, 105.8342);
51+
var centerLocation = new MapPosition(21.028511, 105.804817);
3652
var cameraOptions = new CameraOptions
3753
{
3854
Center = centerLocation,
@@ -43,6 +59,19 @@ private void HandleMoveCamera(object sender, EventArgs e)
4359
new AnimationOptions(3000L));
4460
}
4561

62+
private void HandleCameraEaseTo(object sender, EventArgs e)
63+
{
64+
var centerLocation = new MapPosition(10.762622, 106.660172);
65+
var cameraOptions = new CameraOptions
66+
{
67+
Center = centerLocation,
68+
Zoom = 12,
69+
};
70+
map.CameraController.EaseTo(
71+
cameraOptions,
72+
new AnimationOptions(3000L));
73+
}
74+
4675
public void ApplyQueryAttributes(IDictionary<string, object> query)
4776
{
4877
info = query["example"] as IExampleInfo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace MapboxMauiQs;
2+
3+
class CameraAnimationsExampleInfo : IExampleInfo
4+
{
5+
public string Group => "Lab";
6+
public string Title => "Camera Animations";
7+
public string Subtitle => "Demo how camera animations work.";
8+
public string PageRoute => typeof(CameraAnimationsExample).FullName;
9+
public int GroupIndex => 0;
10+
public int Index => 65;
11+
}

src/qs/MapboxMauiQs/Examples/Lab/65.CameraFlyAnimation/CameraFlyAnimationExampleInfo.cs

-11
This file was deleted.

0 commit comments

Comments
 (0)