1
+ namespace MapboxMauiQs ;
2
+
3
+ public class MovingCarExample : ContentPage , IExamplePage , IQueryAttributable
4
+ {
5
+ MapboxView map ;
6
+ IExampleInfo info ;
7
+ IPosition lastPosition ;
8
+
9
+ public MovingCarExample ( )
10
+ {
11
+ iOSPage . SetUseSafeArea ( this , false ) ;
12
+ Content = new Grid
13
+ {
14
+ Children = {
15
+ ( map = new MapboxView ( ) ) ,
16
+ new HorizontalStackLayout
17
+ {
18
+ VerticalOptions = LayoutOptions . End ,
19
+ HorizontalOptions = LayoutOptions . Center ,
20
+ Margin = new Thickness ( 16 ) ,
21
+ Spacing = 16 ,
22
+ Children =
23
+ {
24
+ new Button
25
+ {
26
+ Text = "Move" ,
27
+ Command = new Command ( Move ) ,
28
+ } ,
29
+ new Button
30
+ {
31
+ Text = "Center" ,
32
+ Command = new Command ( Centralize ) ,
33
+ } ,
34
+ } ,
35
+ } ,
36
+ }
37
+ } ;
38
+
39
+ map . MapReady += Map_MapReady ;
40
+ map . MapLoaded += Map_MapLoaded ;
41
+ }
42
+
43
+ private void Centralize ( object obj )
44
+ {
45
+ if ( carAnnotation is null ) return ;
46
+
47
+ map . CameraController . EaseTo ( new CameraOptions ( )
48
+ {
49
+ Center = carAnnotation . GeometryValue . Coordinates ,
50
+ } ) ;
51
+ }
52
+
53
+ private void Move ( object obj )
54
+ {
55
+ if ( carAnnotation is null ) return ;
56
+
57
+ var currentPosition = carAnnotation . GeometryValue ;
58
+ carAnnotation . GeometryValue = new GeoJSON . Text . Geometry . Point (
59
+ new MapPosition (
60
+ currentPosition . Latitude ( ) + 0.001 ,
61
+ currentPosition . Longitude ( ) + 0.0001
62
+ )
63
+ ) ;
64
+ pointAnnotationManager . UpdateAnnotations ( carAnnotation ) ;
65
+ }
66
+
67
+ public void ApplyQueryAttributes ( IDictionary < string , object > query )
68
+ {
69
+ info = query [ "example" ] as IExampleInfo ;
70
+
71
+ Title = info ? . Title ;
72
+ }
73
+
74
+ private void Map_MapReady ( object sender , EventArgs e )
75
+ {
76
+ var centerLocation = lastPosition = new MapPosition ( 21.0278 , 105.8342 ) ;
77
+ var cameraOptions = new CameraOptions
78
+ {
79
+ Center = centerLocation ,
80
+ Zoom = 14 ,
81
+ } ;
82
+
83
+ map . CameraOptions = cameraOptions ;
84
+ map . MapboxStyle = MapboxStyle . MAPBOX_STREETS ;
85
+ }
86
+
87
+ IPointAnnotationManager pointAnnotationManager ;
88
+ PointAnnotation carAnnotation ;
89
+ private void Map_MapLoaded ( object sender , EventArgs e )
90
+ {
91
+ map . Images = [
92
+ new ResolvedImage ( "CAR" , "ic_car_top" ) ,
93
+ ] ;
94
+
95
+ pointAnnotationManager = map . AnnotationController . CreatePointAnnotationManager ( "CARS" , LayerPosition . Unknown ( ) ) ;
96
+
97
+ carAnnotation = new PointAnnotation ( new GeoJSON . Text . Geometry . Point ( lastPosition ) )
98
+ {
99
+ IconImage = "CAR" ,
100
+ } ;
101
+ pointAnnotationManager . AddAnnotations ( carAnnotation ) ;
102
+ }
103
+ }
0 commit comments