Skip to content

Commit 04698cd

Browse files
committed
- fix event hookup
1 parent 4dea759 commit 04698cd

File tree

5 files changed

+82
-60
lines changed

5 files changed

+82
-60
lines changed

src/libs/Mapbox.Maui/MauiAppBuilderExtensions.cs

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111

1212
public static class MauiAppBuilderExtensions
1313
{
14+
public static void Add<T>(this HashSet<T> set, params T[] items)
15+
{
16+
foreach (var item in items)
17+
{
18+
set.Add(item);
19+
}
20+
}
21+
1422
public static MauiAppBuilder UseMapbox(
1523
this MauiAppBuilder builder,
1624
string accessToken = default)

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

+31-18
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ partial class MapboxViewHandler
1111
private UITapGestureRecognizer mapTapGestureRecognizer;
1212
private UILongPressGestureRecognizer mapLongPressGestureRecognizer;
1313
private XViewportStatusObserver viewportStatusObserver;
14+
private HashSet<TMBCancelable> cancelables;
1415

1516
void RegisterEvents(PlatformView platformView)
1617
{
18+
cancelables ??= new HashSet<TMBCancelable>();
19+
1720
var mapboxView = VirtualView as MapboxView;
1821
if (mapboxView is not null)
1922
{
@@ -31,22 +34,24 @@ void RegisterEvents(PlatformView platformView)
3134

3235
var mapboxMap = mapView.MapboxMap();
3336

34-
mapboxMap.OnCameraChanged(data =>
35-
{
36-
mapboxView?.InvokeCameraChanged(data.CameraState.ToX());
37-
});
38-
mapboxMap.OnMapLoaded(_ =>
39-
{
40-
mapboxView?.InvokeMapLoaded();
41-
});
42-
mapboxMap.OnMapLoadingError(_ =>
43-
{
44-
mapboxView?.InvokeMapLoadingError();
45-
});
46-
mapboxMap.OnStyleLoaded(_ =>
47-
{
48-
mapboxView?.InvokeStyleLoaded();
49-
});
37+
cancelables.Add(
38+
mapboxMap.OnCameraChanged(data =>
39+
{
40+
mapboxView?.InvokeCameraChanged(data.CameraState.ToX());
41+
}),
42+
mapboxMap.OnMapLoaded(_ =>
43+
{
44+
mapboxView?.InvokeMapLoaded();
45+
}),
46+
mapboxMap.OnMapLoadingError(_ =>
47+
{
48+
mapboxView?.InvokeMapLoadingError();
49+
}),
50+
mapboxMap.OnStyleLoaded(_ =>
51+
{
52+
mapboxView?.InvokeStyleLoaded();
53+
})
54+
);
5055

5156
mapTapGestureRecognizer = new UITapGestureRecognizer(HandleMapTapped);
5257
mapView.AddGestureRecognizer(mapTapGestureRecognizer);
@@ -61,8 +66,10 @@ void RegisterEvents(PlatformView platformView)
6166

6267
mapView.Gestures().WeakDelegate = new XTMBGestureManagerDelegate(mapboxView);
6368

64-
mapView.Location().OnLocationChangeWithHandler(HandleLocationChanged);
65-
mapView.Location().OnHeadingChangeWithHandler(HandleHeadingChanged);
69+
cancelables.Add(
70+
mapView.Location().OnLocationChangeWithHandler(HandleLocationChanged),
71+
mapView.Location().OnHeadingChangeWithHandler(HandleHeadingChanged)
72+
);
6673
}
6774

6875
private void HandleHeadingChanged(TMBHeading heading)
@@ -97,6 +104,12 @@ void UnRegisterEvents(PlatformView platformView)
97104
mapboxView.Viewport = null;
98105
}
99106

107+
foreach (var cancelable in cancelables)
108+
{
109+
cancelable?.Dispose();
110+
}
111+
cancelables.Clear();
112+
100113
var mapView = platformView.MapView;
101114
if (mapView == null) return;
102115

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ private static void HandleMapboxStyleChanged(MapboxViewHandler handler, IMapboxV
307307

308308
protected override PlatformView CreatePlatformView()
309309
=> new PlatformView(ACCESS_TOKEN);
310-
310+
311311
protected override void DisconnectHandler(PlatformView platformView)
312312
{
313313
UnRegisterEvents(platformView);

src/qs/MapboxMauiQs/Examples/Camera/69.AdvancedViewportGestures/AdvancedViewportGesturesExample.cs

+40-40
Original file line numberDiff line numberDiff line change
@@ -35,45 +35,6 @@ public void ApplyQueryAttributes(IDictionary<string, object> query)
3535

3636
private async void Map_MapReady(object sender, EventArgs e)
3737
{
38-
//var centerLocation = new MapPosition(37.3230, -122.0322); // Cupertino
39-
//var cameraOptions = new CameraOptions
40-
//{
41-
// Center = centerLocation,
42-
// Zoom = 14,
43-
//};
44-
45-
//map.CameraOptions = cameraOptions;
46-
followPuckViewportState = map.Viewport.MakeFollowPuckViewportState(new FollowPuckViewportStateOptions
47-
{
48-
Bearing = 0,
49-
Padding = new Thickness(200, 0, 0, 0),
50-
});
51-
52-
routePoints = await LoadGeojson();
53-
overviewViewportState = map.Viewport.MakeOverviewViewportState(new OverviewViewportStateOptions
54-
{
55-
Geometry = routePoints,
56-
Padding = 100,
57-
});
58-
59-
var geojsonSource = new GeoJSONSource(GEOJSON_SOURCE_ID)
60-
{
61-
Data = routePoints
62-
};
63-
var lineLayer = new LineLayer(ROUTE_LINE_LAYER_ID)
64-
{
65-
Source = GEOJSON_SOURCE_ID,
66-
LineColor = MAPBOX_BLUE,
67-
LineWidth = 10.0,
68-
LineCap = MapboxMaui.LineCap.Round,
69-
LineJoin = MapboxMaui.LineJoin.Round,
70-
};
71-
map.Sources = [geojsonSource];
72-
map.Layers = [lineLayer];
73-
74-
map.StyleLoaded += Map_StyleLoaded;
75-
map.MapboxStyle = MapboxStyle.TRAFFIC_DAY;
76-
7738
}
7839

7940
private void Map_MapTapped(object sender, MapTappedEventArgs e)
@@ -156,9 +117,48 @@ private void ClearAdvancedGesturesForFollowPuckViewportState()
156117
//map.GestureShoved -= HandleGestureShoved;
157118
}
158119

159-
private void Map_MapLoaded(object sender, EventArgs e)
120+
private async void Map_MapLoaded(object sender, EventArgs e)
160121
{
161122
// Setup Styles, Annotations, etc here
123+
124+
//var centerLocation = new MapPosition(37.3230, -122.0322); // Cupertino
125+
//var cameraOptions = new CameraOptions
126+
//{
127+
// Center = centerLocation,
128+
// Zoom = 14,
129+
//};
130+
131+
//map.CameraOptions = cameraOptions;
132+
followPuckViewportState = map.Viewport.MakeFollowPuckViewportState(new FollowPuckViewportStateOptions
133+
{
134+
Bearing = 0,
135+
Padding = new Thickness(200, 0, 0, 0),
136+
});
137+
138+
routePoints = await LoadGeojson();
139+
overviewViewportState = map.Viewport.MakeOverviewViewportState(new OverviewViewportStateOptions
140+
{
141+
Geometry = routePoints,
142+
Padding = 100,
143+
});
144+
145+
var geojsonSource = new GeoJSONSource(GEOJSON_SOURCE_ID)
146+
{
147+
Data = routePoints
148+
};
149+
var lineLayer = new LineLayer(ROUTE_LINE_LAYER_ID)
150+
{
151+
Source = GEOJSON_SOURCE_ID,
152+
LineColor = MAPBOX_BLUE,
153+
LineWidth = 10.0,
154+
LineCap = MapboxMaui.LineCap.Round,
155+
LineJoin = MapboxMaui.LineJoin.Round,
156+
};
157+
map.Sources = [geojsonSource];
158+
map.Layers = [lineLayer];
159+
160+
map.StyleLoaded += Map_StyleLoaded;
161+
map.MapboxStyle = MapboxStyle.TRAFFIC_DAY;
162162
}
163163

164164
async static Task<LineString> LoadGeojson()

src/qs/MapboxMauiQs/Examples/Lab/67.AddRemoveAnnotations/AddRemoveAnnotationsExample.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,13 @@ private void Map_MapReady(object sender, EventArgs e)
172172
Zoom = 14,
173173
};
174174

175-
map.MapboxStyle = MapboxStyle.MAPBOX_STREETS;
176175
map.CameraOptions = cameraOptions;
177176
}
178177

179178
private void Map_MapLoaded(object sender, EventArgs e)
180179
{
180+
map.MapboxStyle = MapboxStyle.MAPBOX_STREETS;
181+
181182
// Setup Styles, Annotations, etc here
182183
map.Images = [
183184
new ResolvedImage(markerIconId, "red_marker"),

0 commit comments

Comments
 (0)