Skip to content

Commit 9e56ab6

Browse files
committed
- assign CameraOptions and Style at creation
1 parent 3f14345 commit 9e56ab6

File tree

10 files changed

+124
-19
lines changed

10 files changed

+124
-19
lines changed

src/libs/Mapbox.Maui/Mapbox.Maui.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<RepositoryUrl>https://github.com/tuyen-vuduc/mapbox-maui</RepositoryUrl>
6262
<PackageProjectUrl>https://mapbox.tuyen-vuduc.tech</PackageProjectUrl>
6363
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
64-
<PackageVersion>11.7.1-alpha01</PackageVersion>
64+
<PackageVersion>11.7.1-alpha02</PackageVersion>
6565
<PackageReadmeFile>README.md</PackageReadmeFile>
6666
<PackageLicenseFile>LICENSE</PackageLicenseFile>
6767
<PackageIcon>tv-mapbox.png</PackageIcon>
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
namespace MapboxMaui;
22

3-
public record struct CameraOptions
3+
public partial record struct CameraOptions
44
{
55
public IPosition Center { get; set; }
66
public Thickness? Padding { get; set; }
77
public ScreenPosition? Anchor { get; set; }
88
public float? Zoom { get; set; }
99
public float? Bearing { get; set; }
1010
public float? Pitch { get; set; }
11-
}
12-
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Android.OS;
2+
using Android.Runtime;
3+
4+
namespace MapboxMaui;
5+
6+
internal class CameraOptionsParcelable : Java.Lang.Object, IParcelable
7+
{
8+
public CameraOptions CameraOptions { get; private set; }
9+
10+
public CameraOptionsParcelable(CameraOptions cameraOptions)
11+
{
12+
this.CameraOptions = cameraOptions;
13+
}
14+
15+
public void WriteToParcel(Parcel dest, [GeneratedEnum] ParcelableWriteFlags flags)
16+
{
17+
dest.WriteFloat(CameraOptions.Zoom ?? 0);
18+
dest.WriteDouble(CameraOptions.Center.Latitude);
19+
dest.WriteDouble(CameraOptions.Center.Longitude);
20+
dest.WriteFloat(CameraOptions.Bearing ?? 0);
21+
dest.WriteFloat(CameraOptions.Pitch ?? 0);
22+
}
23+
24+
public int DescribeContents()
25+
{
26+
return 0;
27+
}
28+
29+
public static readonly IParcelableCreator CREATOR = new XCreator();
30+
31+
class XCreator : Java.Lang.Object, IParcelableCreator
32+
{
33+
public Java.Lang.Object CreateFromParcel(Parcel source)
34+
{
35+
var zoom = source.ReadFloat();
36+
var latitude = source.ReadDouble();
37+
var longitude = source.ReadDouble();
38+
var bearing = source.ReadFloat();
39+
var pitch = source.ReadFloat();
40+
41+
var cameraOptions = new CameraOptions
42+
{
43+
Zoom = zoom,
44+
Center = new MapPosition(latitude, longitude),
45+
Bearing = bearing,
46+
Pitch = pitch,
47+
};
48+
49+
return new CameraOptionsParcelable(cameraOptions);
50+
}
51+
52+
public Java.Lang.Object[] NewArray(int size)
53+
{
54+
return new CameraOptionsParcelable[size];
55+
}
56+
}
57+
}

src/libs/Mapbox.Maui/Platforms/Android/MapboxFragment.cs

+17
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ public override View OnCreateView(LayoutInflater inflater, ViewGroup container,
5959
}
6060

6161
MapView = new MapView(Context);
62+
63+
var cameraOptionsParcelable = Arguments?
64+
.GetParcelable(nameof(IMapboxView.CameraOptions))
65+
as CameraOptionsParcelable;
66+
if (cameraOptionsParcelable is not null)
67+
{
68+
var cameraOptions = cameraOptionsParcelable.CameraOptions.ToNative();
69+
MapView.MapboxMap.SetCamera(cameraOptions);
70+
}
71+
72+
var styleUri = Arguments?
73+
.GetString(nameof(IMapboxView.MapboxStyle));
74+
if (!string.IsNullOrWhiteSpace(styleUri))
75+
{
76+
MapView.MapboxMap.LoadStyle(styleUri);
77+
}
78+
6279
return MapView;
6380
}
6481

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Com.Mapbox.Maps.Plugins.Gestures.Generated;
1212
using Microsoft.Maui;
1313
using AndroidX.Fragment.App;
14+
using Android.OS;
1415

1516
namespace MapboxMaui;
1617
public partial class MapboxViewHandler
@@ -239,10 +240,18 @@ protected override PlatformView CreatePlatformView()
239240
Id = Android.Views.View.GenerateViewId(),
240241
};
241242
mapboxFragment = new MapboxFragment();
243+
var args = new Bundle();
244+
args.PutParcelable(
245+
nameof(CameraOptions),
246+
new CameraOptionsParcelable(VirtualView.CameraOptions));
247+
mapboxFragment.Arguments = args;
242248

243249
var fragmentManager = MauiContext.Services.GetService<FragmentManager>();
244250
var fragmentTransaction = fragmentManager.BeginTransaction();
245-
fragmentTransaction.Replace(fragmentContainerView.Id, mapboxFragment, $"mapbox-maui-{fragmentContainerView.Id}");
251+
fragmentTransaction.Replace(
252+
fragmentContainerView.Id,
253+
mapboxFragment,
254+
$"mapbox-maui-{fragmentContainerView.Id}");
246255
fragmentTransaction.CommitAllowingStateLoss();
247256
return fragmentContainerView;
248257
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,9 @@ public static TMBOrnamentVisibility ToNative(this OrnamentVisibility value)
304304

305305
public static string ToNative(this MapboxStyle mapboxStyle)
306306
{
307-
return mapboxStyle.Value;
307+
return string.IsNullOrWhiteSpace(mapboxStyle.Value)
308+
? null
309+
: mapboxStyle.Value;
308310
}
309311

310312
public static MBMMapDebugOptions ToNative(this DebugOption option)

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ public class MapViewContainer : UIView
99
{
1010
public MapView MapView { get; private set; }
1111

12-
public MapViewContainer(string accessToken)
12+
public MapViewContainer(
13+
string accessToken,
14+
CameraOptions? cameraOptions,
15+
MapboxStyle mapboxStyle)
1316
: base()
1417
{
1518
if (!string.IsNullOrWhiteSpace(accessToken))
@@ -18,8 +21,10 @@ public MapViewContainer(string accessToken)
1821
}
1922

2023
var mapboxOptions = new MBMMapOptions(null, null, null, null, null, null, 1, null);
24+
var xcameraOptions = cameraOptions?.ToNative();
25+
var styleUri = mapboxStyle.ToNative();
2126
var options = MapInitOptionsFactory
22-
.CreateWithMapOptions(mapboxOptions, null, null, null, 0);
27+
.CreateWithMapOptions(mapboxOptions, xcameraOptions, styleUri, null, 0);
2328

2429
var mapView = MapViewFactory.CreateWithFrame(
2530
CoreGraphics.CGRect.FromLTRB(0, 0, 320, 675),

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,10 @@ private static void HandleMapboxStyleChanged(MapboxViewHandler handler, IMapboxV
306306
}
307307

308308
protected override PlatformView CreatePlatformView()
309-
=> new PlatformView(ACCESS_TOKEN);
309+
=> new PlatformView(
310+
ACCESS_TOKEN,
311+
VirtualView.CameraOptions,
312+
VirtualView.MapboxStyle);
310313

311314
protected override void DisconnectHandler(PlatformView platformView)
312315
{

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,16 @@ public class AdvancedViewportGesturesExample : ContentPage, IExamplePage, IQuery
2020
public AdvancedViewportGesturesExample()
2121
{
2222
iOSPage.SetUseSafeArea(this, false);
23-
Content = map = new MapboxView();
23+
24+
var centerLocation = new MapPosition(21.0245, 105.8412); // Hanoi
25+
Content = map = new MapboxView()
26+
{
27+
CameraOptions = new()
28+
{
29+
Center = centerLocation,
30+
Zoom = 14,
31+
}
32+
};
2433

2534
map.MapReady += Map_MapReady;
2635
map.MapLoaded += Map_MapLoaded;

src/qs/MapboxMauiQs/Examples/Lab/66.GestureSettings/GestureSettingsExample.cs

+13-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,17 @@ public class GestureSettingsExample : ContentPage, IExamplePage, IQueryAttributa
88
public GestureSettingsExample()
99
{
1010
iOSPage.SetUseSafeArea(this, false);
11-
Content = map = new MapboxView();
11+
12+
var centerLocation = new MapPosition(21.028511, 105.804817);
13+
var cameraOptions = new CameraOptions
14+
{
15+
Center = centerLocation,
16+
Zoom = 14,
17+
};
18+
Content = map = new MapboxView()
19+
{
20+
CameraOptions = cameraOptions,
21+
};
1222

1323
var toolbarItem = new ToolbarItem()
1424
{
@@ -17,6 +27,8 @@ public GestureSettingsExample()
1727
};
1828
ToolbarItems.Add(toolbarItem);
1929

30+
31+
2032
map.MapReady += Map_MapReady;
2133
map.MapLoaded += Map_MapLoaded;
2234
}
@@ -45,14 +57,6 @@ public void ApplyQueryAttributes(IDictionary<string, object> query)
4557

4658
private void Map_MapReady(object sender, EventArgs e)
4759
{
48-
var centerLocation = new MapPosition(21.028511, 105.804817);
49-
var cameraOptions = new CameraOptions
50-
{
51-
Center = centerLocation,
52-
Zoom = 14,
53-
};
54-
55-
map.CameraOptions = cameraOptions;
5660
}
5761

5862
private void Map_MapLoaded(object sender, EventArgs e)

0 commit comments

Comments
 (0)