Skip to content

Commit

Permalink
- assign CameraOptions and Style at creation
Browse files Browse the repository at this point in the history
  • Loading branch information
tuyen-vuduc committed Oct 29, 2024
1 parent 3f14345 commit 9e56ab6
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/libs/Mapbox.Maui/Mapbox.Maui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<RepositoryUrl>https://github.com/tuyen-vuduc/mapbox-maui</RepositoryUrl>
<PackageProjectUrl>https://mapbox.tuyen-vuduc.tech</PackageProjectUrl>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageVersion>11.7.1-alpha01</PackageVersion>
<PackageVersion>11.7.1-alpha02</PackageVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageIcon>tv-mapbox.png</PackageIcon>
Expand Down
5 changes: 2 additions & 3 deletions src/libs/Mapbox.Maui/Models/CameraOptions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
namespace MapboxMaui;

public record struct CameraOptions
public partial record struct CameraOptions
{
public IPosition Center { get; set; }
public Thickness? Padding { get; set; }
public ScreenPosition? Anchor { get; set; }
public float? Zoom { get; set; }
public float? Bearing { get; set; }
public float? Pitch { get; set; }
}

}
57 changes: 57 additions & 0 deletions src/libs/Mapbox.Maui/Platforms/Android/CameraOptionsParcelable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Android.OS;
using Android.Runtime;

namespace MapboxMaui;

internal class CameraOptionsParcelable : Java.Lang.Object, IParcelable
{
public CameraOptions CameraOptions { get; private set; }

public CameraOptionsParcelable(CameraOptions cameraOptions)
{
this.CameraOptions = cameraOptions;
}

public void WriteToParcel(Parcel dest, [GeneratedEnum] ParcelableWriteFlags flags)
{
dest.WriteFloat(CameraOptions.Zoom ?? 0);
dest.WriteDouble(CameraOptions.Center.Latitude);
dest.WriteDouble(CameraOptions.Center.Longitude);
dest.WriteFloat(CameraOptions.Bearing ?? 0);
dest.WriteFloat(CameraOptions.Pitch ?? 0);
}

public int DescribeContents()
{
return 0;
}

public static readonly IParcelableCreator CREATOR = new XCreator();

class XCreator : Java.Lang.Object, IParcelableCreator
{
public Java.Lang.Object CreateFromParcel(Parcel source)
{
var zoom = source.ReadFloat();
var latitude = source.ReadDouble();
var longitude = source.ReadDouble();
var bearing = source.ReadFloat();
var pitch = source.ReadFloat();

var cameraOptions = new CameraOptions
{
Zoom = zoom,
Center = new MapPosition(latitude, longitude),
Bearing = bearing,
Pitch = pitch,
};

return new CameraOptionsParcelable(cameraOptions);
}

public Java.Lang.Object[] NewArray(int size)
{
return new CameraOptionsParcelable[size];
}
}
}
17 changes: 17 additions & 0 deletions src/libs/Mapbox.Maui/Platforms/Android/MapboxFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ public override View OnCreateView(LayoutInflater inflater, ViewGroup container,
}

MapView = new MapView(Context);

var cameraOptionsParcelable = Arguments?
.GetParcelable(nameof(IMapboxView.CameraOptions))
as CameraOptionsParcelable;
if (cameraOptionsParcelable is not null)
{
var cameraOptions = cameraOptionsParcelable.CameraOptions.ToNative();
MapView.MapboxMap.SetCamera(cameraOptions);
}

var styleUri = Arguments?
.GetString(nameof(IMapboxView.MapboxStyle));
if (!string.IsNullOrWhiteSpace(styleUri))
{
MapView.MapboxMap.LoadStyle(styleUri);
}

return MapView;
}

Expand Down
11 changes: 10 additions & 1 deletion src/libs/Mapbox.Maui/Platforms/Android/MapboxViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Com.Mapbox.Maps.Plugins.Gestures.Generated;
using Microsoft.Maui;
using AndroidX.Fragment.App;
using Android.OS;

namespace MapboxMaui;
public partial class MapboxViewHandler
Expand Down Expand Up @@ -239,10 +240,18 @@ protected override PlatformView CreatePlatformView()
Id = Android.Views.View.GenerateViewId(),
};
mapboxFragment = new MapboxFragment();
var args = new Bundle();
args.PutParcelable(
nameof(CameraOptions),
new CameraOptionsParcelable(VirtualView.CameraOptions));
mapboxFragment.Arguments = args;

var fragmentManager = MauiContext.Services.GetService<FragmentManager>();
var fragmentTransaction = fragmentManager.BeginTransaction();
fragmentTransaction.Replace(fragmentContainerView.Id, mapboxFragment, $"mapbox-maui-{fragmentContainerView.Id}");
fragmentTransaction.Replace(
fragmentContainerView.Id,
mapboxFragment,
$"mapbox-maui-{fragmentContainerView.Id}");
fragmentTransaction.CommitAllowingStateLoss();
return fragmentContainerView;
}
Expand Down
4 changes: 3 additions & 1 deletion src/libs/Mapbox.Maui/Platforms/iOS/AdditionalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,9 @@ public static TMBOrnamentVisibility ToNative(this OrnamentVisibility value)

public static string ToNative(this MapboxStyle mapboxStyle)
{
return mapboxStyle.Value;
return string.IsNullOrWhiteSpace(mapboxStyle.Value)
? null
: mapboxStyle.Value;
}

public static MBMMapDebugOptions ToNative(this DebugOption option)
Expand Down
9 changes: 7 additions & 2 deletions src/libs/Mapbox.Maui/Platforms/iOS/MapViewContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ public class MapViewContainer : UIView
{
public MapView MapView { get; private set; }

public MapViewContainer(string accessToken)
public MapViewContainer(
string accessToken,
CameraOptions? cameraOptions,
MapboxStyle mapboxStyle)
: base()
{
if (!string.IsNullOrWhiteSpace(accessToken))
Expand All @@ -18,8 +21,10 @@ public MapViewContainer(string accessToken)
}

var mapboxOptions = new MBMMapOptions(null, null, null, null, null, null, 1, null);
var xcameraOptions = cameraOptions?.ToNative();
var styleUri = mapboxStyle.ToNative();
var options = MapInitOptionsFactory
.CreateWithMapOptions(mapboxOptions, null, null, null, 0);
.CreateWithMapOptions(mapboxOptions, xcameraOptions, styleUri, null, 0);

var mapView = MapViewFactory.CreateWithFrame(
CoreGraphics.CGRect.FromLTRB(0, 0, 320, 675),
Expand Down
5 changes: 4 additions & 1 deletion src/libs/Mapbox.Maui/Platforms/iOS/MapboxViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,10 @@ private static void HandleMapboxStyleChanged(MapboxViewHandler handler, IMapboxV
}

protected override PlatformView CreatePlatformView()
=> new PlatformView(ACCESS_TOKEN);
=> new PlatformView(
ACCESS_TOKEN,
VirtualView.CameraOptions,
VirtualView.MapboxStyle);

protected override void DisconnectHandler(PlatformView platformView)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ public class AdvancedViewportGesturesExample : ContentPage, IExamplePage, IQuery
public AdvancedViewportGesturesExample()
{
iOSPage.SetUseSafeArea(this, false);
Content = map = new MapboxView();

var centerLocation = new MapPosition(21.0245, 105.8412); // Hanoi
Content = map = new MapboxView()
{
CameraOptions = new()
{
Center = centerLocation,
Zoom = 14,
}
};

map.MapReady += Map_MapReady;
map.MapLoaded += Map_MapLoaded;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ public class GestureSettingsExample : ContentPage, IExamplePage, IQueryAttributa
public GestureSettingsExample()
{
iOSPage.SetUseSafeArea(this, false);
Content = map = new MapboxView();

var centerLocation = new MapPosition(21.028511, 105.804817);
var cameraOptions = new CameraOptions
{
Center = centerLocation,
Zoom = 14,
};
Content = map = new MapboxView()
{
CameraOptions = cameraOptions,
};

var toolbarItem = new ToolbarItem()
{
Expand All @@ -17,6 +27,8 @@ public GestureSettingsExample()
};
ToolbarItems.Add(toolbarItem);



map.MapReady += Map_MapReady;
map.MapLoaded += Map_MapLoaded;
}
Expand Down Expand Up @@ -45,14 +57,6 @@ public void ApplyQueryAttributes(IDictionary<string, object> query)

private void Map_MapReady(object sender, EventArgs e)
{
var centerLocation = new MapPosition(21.028511, 105.804817);
var cameraOptions = new CameraOptions
{
Center = centerLocation,
Zoom = 14,
};

map.CameraOptions = cameraOptions;
}

private void Map_MapLoaded(object sender, EventArgs e)
Expand Down

0 comments on commit 9e56ab6

Please sign in to comment.