|
1 | 1 |
|
2 | 2 |
|
| 3 | +using Com.Mapbox.Functions; |
| 4 | + |
3 | 5 | namespace MapboxMaui;
|
4 | 6 |
|
5 | 7 | partial class MapboxViewHandler : IMapboxController
|
@@ -39,4 +41,81 @@ public ScreenPosition GetScreenPosition(IPosition position)
|
39 | 41 | var coords = mapView.MapboxMap.PixelForCoordinate(position.ToGeoPoint());
|
40 | 42 | return coords.ToX();
|
41 | 43 | }
|
| 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 | + } |
42 | 121 | }
|
0 commit comments