-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMapboxViewHandler.cs
273 lines (213 loc) · 8.85 KB
/
MapboxViewHandler.cs
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
using PlatformView = AndroidX.Fragment.App.FragmentContainerView;
using MapboxMapsStyle = Com.Mapbox.Maps.Style;
using Com.Mapbox.Maps;
using Com.Mapbox.Maps.Plugins.Scalebar;
using Microsoft.Maui.Platform;
using Android.Content;
using Android.Graphics;
using GeoJSON.Text;
namespace MapboxMaui;
public partial class MapboxViewHandler
{
MapboxFragment mapboxFragment;
private static void HandleLightChanged(MapboxViewHandler handler, IMapboxView view)
{
var mapView = handler.GetMapView();
if (mapView == null) return;
if (view.Light == null) return;
mapView.MapboxMap.Style.SetStyleLights(
view.Light.ToPlatformValue(true)
);
}
private static void HandleLayersChanged(MapboxViewHandler handler, IMapboxView view)
{
var mapView = handler.GetMapView();
if (mapView == null) return;
if (view.Layers == null) return;
foreach (var layer in view.Layers)
{
var value = layer.ToPlatformValue(true);
if (mapView.MapboxMap.Style.StyleLayerExists(layer.Id))
{
mapView.MapboxMap.Style.SetStyleLayerProperties(layer.Id, value);
continue;
}
mapView.MapboxMap.Style.AddStyleLayer(
value,
layer.LayerPosition.ToPlatformValue()
);
}
}
private static void HandleImagesChanged(MapboxViewHandler handler, IMapboxView view)
{
var mapView = handler.GetMapView();
if (mapView == null) return;
if (view.Images == null) return;
foreach (var ximage in view.Images)
{
if (!string.IsNullOrWhiteSpace(ximage.Name))
{
var resourceId = mapView.Resources.GetDrawableId(AppInfo.PackageName, ximage.Name);
if (resourceId == 0) continue;
var bitmap = BitmapFactory.DecodeResource(mapView.Resources, resourceId);
mapView.MapboxMap.Style.AddImage(
ximage.Id,
bitmap,
ximage.Sdf);
}
// TODO handle other image types
}
}
private static void HandleTerrainChanged(MapboxViewHandler handler, IMapboxView view)
{
var mapView = handler.GetMapView();
if (mapView == null) return;
if (view.Terrain == null) return;
var platformValue = view.Terrain.ToPlatformValue();
platformValue.BindTo(mapView.MapboxMap.Style);
}
private static void HandleSourcesChanged(MapboxViewHandler handler, IMapboxView view)
{
var mapView = handler.GetMapView();
if (mapView == null) return;
if (view.Sources == null) return;
var style = mapView.MapboxMap.Style;
foreach (var source in view.Sources)
{
var sourceExists = style.StyleSourceExists(source.Id);
var platformValue = source.ToPlatformValue();
if (sourceExists)
{
style.SetStyleSourceProperties(source.Id, platformValue);
}
else
{
style.AddStyleSource(source.Id, platformValue);
}
if (source is Styles.GeoJSONSource geojsonSource)
{
HandleGeoJSONSourceChanged(style, source.Id, geojsonSource.Data);
continue;
}
if (!source.VolatileProperties.Any()) continue;
var setStyleSourcePropertiesResult = mapView.MapboxMap.Style.SetStyleSourceProperties(
source.Id,
source.GetVolatileProperties()
);
if (setStyleSourcePropertiesResult.IsError)
{
System.Diagnostics.Debug.WriteLine(setStyleSourcePropertiesResult.Error);
}
}
}
private static void HandleGeoJSONSourceChanged(MapboxMapsStyle style, string sourceId, IGeoJSONObject data)
{
var xdata = data switch
{
Styles.RawGeoJSONObject raw => GeoJSONSourceData.ValueOf(raw.Data),
GeoJSON.Text.Geometry.IGeometryObject geometry => GeoJSONSourceData.ValueOf(geometry.ToNative()),
_ => null,
};
if (xdata is null) return;
// TODO Correctly define dataID
var dataId = Guid.NewGuid().ToString();
var setStyleGeoJSONSourceDataResult = style.SetStyleGeoJSONSourceData(sourceId, dataId, xdata);
if (setStyleGeoJSONSourceDataResult.IsError)
{
System.Diagnostics.Debug.WriteLine(setStyleGeoJSONSourceDataResult.Error);
}
}
private static void HandleCameraOptionsChanged(MapboxViewHandler handler, IMapboxView view)
{
var cameraOptions = view.CameraOptions.ToNative();
handler.GetMapView()?.MapboxMap.SetCamera(cameraOptions);
}
private static void HandleDebugOptionsChanged(MapboxViewHandler handler, IMapboxView view)
{
if (view.DebugOptions == null) return;
var mapView = handler.GetMapView();
if (mapView == null) return;
var debugOptions = mapView.MapboxMap.Debug;
handler.GetMapView().MapboxMap.SetDebug(debugOptions, false);
debugOptions = view.DebugOptions.ToNative();
handler.GetMapView().MapboxMap.SetDebug(debugOptions, true);
}
private static void HandleScaleBarVisibilityChanged(MapboxViewHandler handler, IMapboxView view)
{
var mapView = handler.GetMapView();
if (mapView?.MapboxMap.Style?.IsStyleLoaded != true) return;
var scaleBarPlugin = ScaleBarUtils.GetScaleBar(mapView);
if (view.ScaleBarVisibility == OrnamentVisibility.Hidden)
{
scaleBarPlugin.Enabled = false;
return;
}
scaleBarPlugin.Enabled = true;
}
private static void HandleMapboxStyleChanged(MapboxViewHandler handler, IMapboxView view)
{
var styleUri = view.MapboxStyle.ToNative();
if (string.IsNullOrWhiteSpace(styleUri))
{
styleUri = MapboxMapsStyle.MapboxStreets;
}
handler.GetMapView()?.MapboxMap.LoadStyleUri(styleUri);
}
protected override PlatformView CreatePlatformView()
{
var mainActivity = (MauiAppCompatActivity)Context.GetActivity();
var fragmentContainerView = new PlatformView(Context)
{
Id = Android.Views.View.GenerateViewId(),
};
mapboxFragment = new MapboxFragment();
mapboxFragment.MapViewReady += HandleMapViewReady;
mapboxFragment.StyleLoaded += HandleStyleLoaded;
mapboxFragment.MapLoaded += HandleMapLoaded;
mapboxFragment.MapClicked += HandleMapClicked;
mapboxFragment.MapLongClicked += HandleMapLongClicked;
var fragmentTransaction = mainActivity.SupportFragmentManager.BeginTransaction();
fragmentTransaction.Replace(fragmentContainerView.Id, mapboxFragment, $"mapbox-maui-{fragmentContainerView.Id}");
fragmentTransaction.CommitAllowingStateLoss();
return fragmentContainerView;
}
protected override void ConnectHandler(PlatformView platformView)
{
base.ConnectHandler(platformView);
if (VirtualView is MapboxView mapboxView)
{
mapboxView.AnnotationController = this;
mapboxView.QueryManager = this;
mapboxView.MapboxController = this;
}
}
protected override void DisconnectHandler(PlatformView platformView)
{
if (mapboxFragment != null)
{
mapboxFragment.MapViewReady -= HandleMapViewReady;
mapboxFragment.StyleLoaded -= HandleStyleLoaded;
mapboxFragment.MapLoaded -= HandleMapLoaded;
mapboxFragment.MapClicked -= HandleMapClicked;
mapboxFragment.MapLongClicked -= HandleMapLongClicked;
mapboxFragment.Dispose();
mapboxFragment = null;
}
if (VirtualView is MapboxView mapboxView)
{
mapboxView.AnnotationController = null;
mapboxView.QueryManager = null;
}
base.DisconnectHandler(platformView);
}
private void HandleMapViewReady(MapView view)
=> (VirtualView as MapboxView)?.InvokeMapReady();
private void HandleMapLoaded(MapView view)
=> (VirtualView as MapboxView)?.InvokeMapLoaded();
private void HandleStyleLoaded(MapView view)
=> (VirtualView as MapboxView)?.InvokeStyleLoaded();
private void HandleMapClicked(MapTappedPosition point)
=> (VirtualView as MapboxView)?.InvokeMapTapped(point);
private void HandleMapLongClicked(MapTappedPosition point)
=> (VirtualView as MapboxView)?.InvokeMapLongTapped(point);
}