1
+ using System . Text . Json ;
2
+ using GeoJSON . Text . Feature ;
3
+ using MapboxMaui ;
4
+ using MapboxMaui . Expressions ;
5
+ using MapboxMaui . Styles ;
6
+ using iOSPage = Microsoft . Maui . Controls . PlatformConfiguration . iOSSpecific . Page ;
7
+ namespace MapboxMauiQs ;
8
+
9
+ public class AddMarkersSymbolExample : ContentPage , IExamplePage , IQueryAttributable
10
+ {
11
+ private static class Constants
12
+ {
13
+ public const string ICON_KEY = "icon_key" ;
14
+ public const string BLUE_MARKER_PROPERTY = "icon_blue_property" ;
15
+ public const string RED_MARKER_PROPERTY = "icon_red_property" ;
16
+ public const string BLUE_ICON_ID = "blue" ;
17
+ public const string RED_ICON_ID = "red" ;
18
+ public const string SOURCE_ID = "source_id" ;
19
+ public const string LAYER_ID = "layer_id" ;
20
+ }
21
+
22
+ MapboxView map ;
23
+ IExampleInfo info ;
24
+
25
+ public AddMarkersSymbolExample ( )
26
+ {
27
+ iOSPage . SetUseSafeArea ( this , false ) ;
28
+ Content = map = new MapboxView ( ) ;
29
+
30
+ map . MapReady += Map_MapReady ;
31
+ map . MapLoaded += Map_MapLoaded ;
32
+ }
33
+
34
+ public void ApplyQueryAttributes ( IDictionary < string , object > query )
35
+ {
36
+ info = query [ "example" ] as IExampleInfo ;
37
+
38
+ Title = info ? . Title ;
39
+ }
40
+
41
+ private void Map_MapReady ( object sender , EventArgs e )
42
+ {
43
+ var centerLocation = new Point ( 55.70651 , 12.554729 ) ;
44
+ var cameraOptions = new CameraOptions
45
+ {
46
+ Center = centerLocation ,
47
+ Zoom = 8 ,
48
+ } ;
49
+
50
+ map . CameraOptions = cameraOptions ;
51
+ map . MapboxStyle = MapboxStyle . MAPBOX_STREETS ;
52
+ }
53
+
54
+ private void Map_MapLoaded ( object sender , EventArgs e )
55
+ {
56
+ map . Images = new [ ] {
57
+ new ResolvedImage ( Constants . BLUE_ICON_ID , "blue_marker_view" ) ,
58
+ new ResolvedImage ( Constants . RED_ICON_ID , "red_marker" ) ,
59
+ } ;
60
+
61
+ var features = new List < Feature > ( ) ;
62
+
63
+ var feature = new Feature (
64
+ new GeoJSON . Text . Geometry . Point (
65
+ new GeoJSON . Text . Geometry . Position ( latitude : 55.608166 , longitude : 12.65147 )
66
+ ) ,
67
+ new Dictionary < string , object > {
68
+ { Constants . ICON_KEY , Constants . BLUE_MARKER_PROPERTY }
69
+ }
70
+ ) ;
71
+ features . Add ( feature ) ;
72
+
73
+ var feature1 = new Feature (
74
+ new GeoJSON . Text . Geometry . Point (
75
+ new GeoJSON . Text . Geometry . Position ( latitude : 55.70651 , longitude : 12.554729 )
76
+ ) ,
77
+ new Dictionary < string , object > {
78
+ { Constants . ICON_KEY , Constants . RED_MARKER_PROPERTY }
79
+ }
80
+ ) ;
81
+ features . Add ( feature1 ) ;
82
+
83
+ var geoJSONString = JsonSerializer . Serialize ( new FeatureCollection ( features ) ) ;
84
+ var source = new GeoJSONSource ( Constants . SOURCE_ID )
85
+ {
86
+ Data = new RawGeoJSONObject ( geoJSONString ) ,
87
+ } ;
88
+
89
+ map . Sources = new [ ] { source } ;
90
+
91
+ var rotateExpression = DslExpression . match (
92
+ DslExpression . get ( Constants . ICON_KEY ) ,
93
+ Constants . BLUE_MARKER_PROPERTY ,
94
+ 45 ,
95
+ 0
96
+ ) ;
97
+ var imageExpression = DslExpression . match (
98
+ DslExpression . get ( Constants . ICON_KEY ) ,
99
+ Constants . BLUE_MARKER_PROPERTY ,
100
+ Constants . BLUE_ICON_ID ,
101
+ Constants . RED_MARKER_PROPERTY ,
102
+ Constants . RED_ICON_ID ,
103
+ Constants . RED_ICON_ID
104
+ ) ;
105
+
106
+ var layer = new SymbolLayer ( id : Constants . LAYER_ID )
107
+ {
108
+ Source = Constants . SOURCE_ID ,
109
+ IconImage = ( PropertyValue < ResolvedImage > ) imageExpression ,
110
+ IconAnchor = IconAnchor . bottom ,
111
+ IconAllowOverlap = false ,
112
+ IconRotate = ( PropertyValue < double > ) rotateExpression
113
+ } ;
114
+
115
+ map . Layers = new [ ] { layer } ;
116
+ }
117
+ }
0 commit comments