Skip to content

Commit 7f5dc68

Browse files
committed
- Use ContentView for ViewAnnotation instead
1 parent e00d491 commit 7f5dc68

File tree

6 files changed

+71
-50
lines changed

6 files changed

+71
-50
lines changed

src/libs/Mapbox.Maui/IMapboxView.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public partial interface IMapboxView : IView
3030

3131
Light Light { get; set; }
3232

33-
DataTemplate DefaultViewAnnotationTemplate { get; set; }
33+
ContentView AnnotationView { get; set; }
3434
IViewAnnotationController ViewAnnotationController { get; }
3535

3636
IAnnotationController AnnotationController { get; }

src/libs/Mapbox.Maui/MapboxView.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public MapboxStyle MapboxStyle
223223
set => SetValue(MapboxStyleProperty, value);
224224
}
225225

226-
public DataTemplate DefaultViewAnnotationTemplate { get; set; }
226+
public ContentView AnnotationView { get; set; }
227227
public IViewAnnotationController ViewAnnotationController { get; internal set; }
228228
public IAnnotationController AnnotationController { get; internal set; }
229229
public IMapFeatureQueryable QueryManager { get; internal set; }

src/libs/Mapbox.Maui/Models/ViewAnnotations/IViewAnnotationController.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
public interface IViewAnnotationController
44
{
5-
public void AddViewAnnotation(ViewAnnotationOptions options, DataTemplate dataTemplate = default);
5+
public void AddViewAnnotation(ViewAnnotationOptions options, ContentView contentView = default);
66
public void RemoveAllViewAnnotations();
77
}
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
11

2+
using Android.Content;
3+
using Android.Views;
24
using MapboxMaui.ViewAnnotations;
5+
using Microsoft.Maui.Platform;
36

47
namespace MapboxMaui;
58

69
partial class MapboxViewHandler : IViewAnnotationController
710
{
8-
public void AddViewAnnotation(ViewAnnotationOptions options, DataTemplate dataTemplate = null)
11+
public void AddViewAnnotation(ViewAnnotationOptions options, ContentView contentView = default)
912
{
1013
var mapView = mapboxFragment?.MapView;
1114

1215
if (mapView == null) return;
1316

14-
dataTemplate = dataTemplate ?? VirtualView.DefaultViewAnnotationTemplate;
17+
contentView = contentView ?? VirtualView.AnnotationView;
1518

16-
if (dataTemplate == null)
19+
if (contentView == null)
1720
{
1821
throw new InvalidOperationException("DataTemplate must be provided eiher via this method parameter or via DefaultViewAnnotationTemplate");
1922
}
2023

21-
var xview = (View)dataTemplate.CreateContent();
22-
xview.Parent = VirtualView as Element;
23-
xview.BindingContext = options;
24-
xview.HeightRequest = options.Height ?? xview.HeightRequest;
25-
xview.WidthRequest = options.Width ?? xview.WidthRequest;
24+
contentView.Parent = VirtualView as Element;
25+
contentView.BindingContext = options;
2626

27-
var platformHandler = TemplateHelpers.GetHandler(
28-
xview,
29-
VirtualView.Handler.MauiContext);
30-
platformHandler.PlatformView.LayoutParameters = new Android.Views.ViewGroup.LayoutParams(
31-
(int) options.Width.Value.PointToPixel(),
32-
(int)options.Height.Value.PointToPixel()
33-
);
27+
var handler = contentView.ToHandler(MauiContext);
28+
29+
var viewGroup = new ContentViewGroup(Context)
30+
{
31+
LayoutParameters = new ViewGroup.LayoutParams(
32+
ViewGroup.LayoutParams.WrapContent,
33+
ViewGroup.LayoutParams.WrapContent),
34+
CrossPlatformLayout = handler.VirtualView as ICrossPlatformLayout,
35+
};
3436

3537
mapView.ViewAnnotationManager.AddViewAnnotation(
36-
platformHandler.PlatformView,
38+
viewGroup,
3739
options.ToPlatform());
3840
}
3941

@@ -45,4 +47,26 @@ public void RemoveAllViewAnnotations()
4547

4648
mapView.ViewAnnotationManager.RemoveAllViewAnnotations();
4749
}
50+
51+
class ViewAnnotationView : ViewGroup
52+
{
53+
public ViewAnnotationView(Context context)
54+
: base(context)
55+
{
56+
57+
}
58+
59+
protected override void OnLayout(bool changed, int l, int t, int r, int b)
60+
{
61+
if (ChildCount == 0) return;
62+
63+
var view = this.GetChildAt(0);
64+
view.Layout(l, t, r, b);
65+
}
66+
67+
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
68+
{
69+
base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
70+
}
71+
}
4872
}

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

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11

22
using MapboxMapsObjC;
33
using MapboxMaui.ViewAnnotations;
4+
using Microsoft.Maui.Platform;
45

56
namespace MapboxMaui;
67

78
partial class MapboxViewHandler : IViewAnnotationController
89
{
9-
public void AddViewAnnotation(ViewAnnotationOptions options, DataTemplate dataTemplate = null)
10+
public void AddViewAnnotation(ViewAnnotationOptions options, Microsoft.Maui.Controls.ContentView contentView = default)
1011
{
1112
var mapView = PlatformView.MapView;
1213

1314
if (mapView == null) return;
1415

15-
dataTemplate = dataTemplate ?? VirtualView.DefaultViewAnnotationTemplate;
16+
contentView = contentView ?? VirtualView.AnnotationView;
1617

17-
if (dataTemplate == null)
18+
if (contentView == null)
1819
{
1920
throw new InvalidOperationException("DataTemplate must be provided eiher via this method parameter or via DefaultViewAnnotationTemplate");
2021
}
2122

22-
var xview = (View)dataTemplate.CreateContent();
23-
xview.Parent = VirtualView as Element;
24-
xview.BindingContext = options;
25-
xview.HeightRequest = options.Height ?? xview.HeightRequest;
26-
xview.WidthRequest = options.Width ?? xview.WidthRequest;
23+
contentView.Parent = VirtualView as Element;
24+
contentView.BindingContext = options;
2725

28-
var platformHandler = TemplateHelpers.GetHandler(
29-
xview,
30-
VirtualView.Handler.MauiContext);
26+
var platformHandler = contentView.ToHandler(
27+
VirtualView.Handler.MauiContext
28+
);
3129

3230
mapView
3331
.ViewAnnotations()

src/qs/MapboxMauiQs/Examples/MarkersAndCallouts/ViewAnnotations/64.ViewAnnotationWithPointAnnotation/ViewAnnotationWithPointAnnotationExample.cs

+19-20
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public ViewAnnotationWithPointAnnotationExample()
2121
iOSPage.SetUseSafeArea(this, false);
2222
Content = map = new MapboxView();
2323

24-
map.DefaultViewAnnotationTemplate = new DataTemplate(typeof(SimpleViewAnnodationView));
24+
map.AnnotationView = new SimpleViewAnnodationView();
2525

2626
map.MapReady += Map_MapReady;
2727
map.MapLoaded += Map_MapLoaded;
@@ -105,23 +105,20 @@ private void AddPointAnnotationAt(IPosition value)
105105
pointAnnotationManager.AddAnnotations(pointAnnotation);
106106
}
107107

108-
class SimpleViewAnnodationView : Grid
108+
class SimpleViewAnnodationView : ContentView
109109
{
110110
public SimpleViewAnnodationView()
111111
{
112-
RowDefinitions.Add(new RowDefinition
112+
var grid = new Grid()
113113
{
114-
Height = 16,
115-
});
116-
RowDefinitions.Add(new RowDefinition
117-
{
118-
Height = 48,
119-
});
120-
RowDefinitions.Add(new RowDefinition
121-
{
122-
Height = 32,
123-
});
124-
BackgroundColor = Colors.Purple;
114+
RowDefinitions =
115+
{
116+
new() { Height = 16 },
117+
new() { Height = 48 },
118+
new() { Height = 32 },
119+
},
120+
BackgroundColor = Colors.Purple,
121+
};
125122

126123
var label = new Label
127124
{
@@ -156,14 +153,16 @@ public SimpleViewAnnodationView()
156153
HorizontalOptions = LayoutOptions.Center,
157154
};
158155

159-
Add(label);
160-
Add(btnClose);
161-
Add(btnSelect);
156+
grid.Add(label, row: 1);
157+
grid.Add(btnClose, row: 0);
158+
grid.Add(btnSelect, row: 2);
162159

163-
Grid.SetRow(btnClose, 0);
164160
Grid.SetRowSpan(btnClose, 2);
165-
Grid.SetRow(label, 1);
166-
Grid.SetRow(btnSelect, 2);
161+
162+
Content = grid;
163+
164+
HeightRequest = 144;
165+
WidthRequest = 168;
167166
}
168167
}
169168

0 commit comments

Comments
 (0)