Skip to content

Commit 4a0e03e

Browse files
committed
- add CameraForCoordinates
1 parent 6598c49 commit 4a0e03e

File tree

4 files changed

+100
-4
lines changed

4 files changed

+100
-4
lines changed

src/libs/Mapbox.Maui/IMapboxView.cs

+15
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@ public interface IMapboxController
8686
IPosition GetMapPosition(ScreenPosition position);
8787
CoordinateBounds GetCoordinateBoundsForCamera(CameraOptions cameraOptions);
8888
ScreenPosition GetScreenPosition(IPosition position);
89+
CameraOptions? CameraForCoordinates(
90+
IEnumerable<MapPosition> coordinates,
91+
CameraOptions? cameraOptions = default,
92+
Thickness? coordinatesPadding = default,
93+
double? maxZoom = default,
94+
ScreenPosition? offset = default
95+
);
96+
void CameraForCoordinates(
97+
IEnumerable<MapPosition> coordinates,
98+
Action<CameraOptions?> completion,
99+
CameraOptions? cameraOptions = default,
100+
Thickness? coordinatesPadding = default,
101+
double? maxZoom = default,
102+
ScreenPosition? offset = default
103+
);
89104
}
90105

91106
public class MapTappedEventArgs : EventArgs

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.5.1-alpha02</PackageVersion>
64+
<PackageVersion>11.5.1-alpha03</PackageVersion>
6565
<PackageReadmeFile>README.md</PackageReadmeFile>
6666
<PackageLicenseFile>LICENSE</PackageLicenseFile>
6767
<PackageIcon>tv-mapbox.png</PackageIcon>

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using Android.Animation;
2+
using Android.App;
3+
using Com.Mapbox.Functions;
24
using Com.Mapbox.Maps.Plugins.Animation;
35
using MapboxMaui.Camera;
46

@@ -12,7 +14,7 @@ public CameraOptions CameraState
1214
{
1315
var mapView = mapboxFragment?.MapView;
1416

15-
if (mapView == null) return default;
17+
if (mapView is null) return default;
1618

1719
return mapView.MapboxMap.CameraState.ToX();
1820
}
@@ -22,7 +24,7 @@ public void EaseTo(CameraOptions cameraOptions, AnimationOptions animationOption
2224
{
2325
var mapView = mapboxFragment?.MapView;
2426

25-
if (mapView == null) return;
27+
if (mapView is null) return;
2628

2729
var xcameraOptions = cameraOptions.ToNative();
2830
var xanimationOptions = animationOptions?.ToNative();
@@ -41,7 +43,7 @@ public void FlyTo(CameraOptions cameraOptions, AnimationOptions animationOptions
4143
{
4244
var mapView = mapboxFragment?.MapView;
4345

44-
if (mapView == null) return;
46+
if (mapView is null) return;
4547

4648
var xcameraOptions = cameraOptions.ToNative();
4749
var xanimationOptions = animationOptions?.ToNative();

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

+79
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11

22

3+
using Com.Mapbox.Functions;
4+
35
namespace MapboxMaui;
46

57
partial class MapboxViewHandler : IMapboxController
@@ -39,4 +41,81 @@ public ScreenPosition GetScreenPosition(IPosition position)
3941
var coords = mapView.MapboxMap.PixelForCoordinate(position.ToGeoPoint());
4042
return coords.ToX();
4143
}
44+
45+
/**
46+
* Convenience method that returns the [CameraOptions] object for given parameters.
47+
*
48+
* Note: if the render thread did not yet calculate the size of the map (due to initialization or map resizing) - empty [CameraOptions] will be returned.
49+
* Emptiness could be checked with [CameraOptions.isEmpty]. Consider using asynchronous overloaded method.
50+
*
51+
* @param coordinates The `coordinates` representing the bounds of the camera.
52+
* @param camera The [CameraOptions] which will be applied before calculating the camera for the coordinates. If any of the fields in [CameraOptions] are not provided then the current value from the map for that field will be used.
53+
* @param coordinatesPadding The amount of padding in pixels to add to the given `coordinates`.
54+
* Note: This padding is not applied to the map but to the coordinates provided. If you want to apply padding to the map use param `camera`.
55+
* @param maxZoom The maximum zoom level allowed in the returned camera options.
56+
* @param offset The center of the given bounds relative to map center in pixels.
57+
*
58+
* @return The [CameraOptions] object representing the provided parameters if the map size was calculated and empty [CameraOptions] otherwise, see [CameraOptions.isEmpty].
59+
* Also empty [CameraOptions] are returned in case of an internal error.
60+
*/
61+
public CameraOptions? CameraForCoordinates(
62+
IEnumerable<MapPosition> coordinates,
63+
CameraOptions? cameraOptions = null,
64+
Thickness? coordinatesPadding = null,
65+
double? maxZoom = null,
66+
ScreenPosition? offset = null)
67+
{
68+
var mapView = mapboxFragment?.MapView;
69+
70+
if (mapView is null) return default;
71+
72+
var result = mapView.MapboxMap.CameraForCoordinates(
73+
coordinates?.Select(x => x.ToGeoPoint()).ToList(),
74+
cameraOptions?.ToNative(),
75+
coordinatesPadding?.ToNative(),
76+
maxZoom?.ToNative(),
77+
offset?.ToScreenCoordinate()
78+
);
79+
return result.ToX();
80+
}
81+
82+
/**
83+
* Convenience method that returns the [CameraOptions] object for given parameters.
84+
*
85+
* @param coordinates The `coordinates` representing the bounds of the camera.
86+
* @param camera The [CameraOptions] which will be applied before calculating the camera for the coordinates. If any of the fields in [CameraOptions] are not provided then the current value from the map for that field will be used.
87+
* @param coordinatesPadding The amount of padding in pixels to add to the given `coordinates`.
88+
* Note: This padding is not applied to the map but to the coordinates provided. If you want to apply padding to the map use param `camera`.
89+
* @param maxZoom The maximum zoom level allowed in the returned camera options.
90+
* @param offset The center of the given bounds relative to map center in pixels.
91+
* @param completion Callback returning the [CameraOptions] object representing the provided parameters. Those [CameraOptions] always take into account actual MapView size and may return empty ([CameraOptions.isEmpty]) options only if an internal error has occurred.
92+
*/
93+
public void CameraForCoordinates(
94+
IEnumerable<MapPosition> coordinates,
95+
Action<CameraOptions?> completion,
96+
CameraOptions? cameraOptions = null,
97+
Thickness? coordinatesPadding = null,
98+
double? maxZoom = null,
99+
ScreenPosition? offset = null)
100+
{
101+
var mapView = mapboxFragment?.MapView;
102+
103+
if (mapView is null)
104+
{
105+
completion(null);
106+
return;
107+
}
108+
109+
mapView.MapboxMap.CameraForCoordinates(
110+
coordinates?.Select(x => x.ToGeoPoint()).ToList(),
111+
cameraOptions?.ToNative(),
112+
coordinatesPadding?.ToNative(),
113+
maxZoom?.ToNative(),
114+
offset?.ToScreenCoordinate(),
115+
new Function1Action<Com.Mapbox.Maps.CameraOptions>((result) =>
116+
{
117+
completion?.Invoke(result.ToX());
118+
})
119+
);
120+
}
42121
}

0 commit comments

Comments
 (0)