Skip to content

Commit

Permalink
[compose] Expose MapViewportState.cameraForCoordinates (#2835)
Browse files Browse the repository at this point in the history
* [compose] Expose several methods in MapViewportState

* Add example

* Remove couple methods
  • Loading branch information
kiryldz authored Nov 5, 2024
1 parent 69eb845 commit cf57218
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Mapbox welcomes participation and contributions from everyone.

# main
## Features ✨ and improvements 🏁
* [compose] Expose `MapViewportState.cameraForCoordinates` method.

# 11.8.0
## Breaking changes ⚠️
* Change the signature of experimental `MapboxMap.queryRenderedFeatures(RenderedQueryGeometry, TypedFeaturesetDescriptor, Value?, QueryRenderedFeaturesetFeaturesCallback)` to `MapboxMap.queryRenderedFeatures(TypedFeaturesetDescriptor, RenderedQueryGeometry?, Value?, QueryRenderedFeaturesetFeaturesCallback)`. `RenderedQueryGeometry` being NULL is equivalent to passing a bounding box encompassing the entire map viewport.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -22,6 +23,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.mapbox.geojson.Point
import com.mapbox.maps.EdgeInsets
import com.mapbox.maps.MapboxExperimental
import com.mapbox.maps.compose.testapp.ExampleScaffold
import com.mapbox.maps.compose.testapp.ui.theme.MapboxMapComposeTheme
Expand Down Expand Up @@ -53,22 +55,38 @@ public class InteractionsActivity : ComponentActivity() {
}

val mapState = rememberMapState()
val mapViewportState = rememberMapViewportState {
setCameraOptions(
cameraOptions {
center(Point.fromLngLat(-73.99, 40.72))
zoom(11.0)
pitch(45.0)
}
)
}

LaunchedEffect(selectedPriceLabel) {
selectedPriceLabel?.let {
val camera = mapViewportState.cameraForCoordinates(
listOf(it.geometry as Point),
cameraOptions {
padding(
EdgeInsets(100.0, 100.0, 100.0, 100.0)
)
},
maxZoom = 11.0
)
mapViewportState.flyTo(camera)
}
}

MapboxMapComposeTheme {
ExampleScaffold {
Box(modifier = Modifier.fillMaxSize()) {
MapboxMap(
modifier = Modifier.fillMaxSize(),
mapState = mapState,
mapViewportState = rememberMapViewportState {
setCameraOptions(
cameraOptions {
center(Point.fromLngLat(-73.99, 40.72))
zoom(11.0)
pitch(45.0)
}
)
},
mapViewportState = mapViewportState,
style = {
GenericStyle(
style = "asset://fragment-realestate-NY.json",
Expand Down Expand Up @@ -102,12 +120,12 @@ public class InteractionsActivity : ComponentActivity() {
selectedPriceLabel?.let { actualSelectedPriceLabel ->
Text(
modifier = Modifier
.wrapContentWidth()
.align(Alignment.BottomCenter)
.padding(40.dp)
.border(2.dp, MaterialTheme.colors.secondary, CircleShape)
.background(MaterialTheme.colors.primary, CircleShape)
.padding(10.dp),
.wrapContentWidth()
.align(Alignment.BottomCenter)
.padding(40.dp)
.border(2.dp, MaterialTheme.colors.secondary, CircleShape)
.background(MaterialTheme.colors.primary, CircleShape)
.padding(10.dp),
textAlign = TextAlign.Center,
color = Color.White,
text = "Feature with id: ${actualSelectedPriceLabel.id!!}; active: $isActive",
Expand Down
3 changes: 3 additions & 0 deletions extension-compose/api/Release/metalava.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ package com.mapbox.maps.extension.compose.animation.viewport {

@androidx.compose.runtime.Stable public final class MapViewportState {
ctor public MapViewportState(com.mapbox.maps.CameraState initialCameraState = com.mapbox.maps.extension.compose.animation.viewport.MapViewportState.INIT_CAMERA_STATE);
method @UiThread public suspend Object? cameraForCoordinates(java.util.List<com.mapbox.geojson.Point> coordinates, com.mapbox.maps.CameraOptions camera = cameraOptions({

}), com.mapbox.maps.EdgeInsets? coordinatesPadding = null, Double? maxZoom = null, com.mapbox.maps.ScreenCoordinate? offset = null, kotlin.coroutines.Continuation<? super com.mapbox.maps.CameraOptions> = null);
method public void easeTo(com.mapbox.maps.CameraOptions cameraOptions, com.mapbox.maps.plugin.animation.MapAnimationOptions? animationOptions = null, com.mapbox.maps.plugin.viewport.CompletionListener? completionListener = null);
method public void flyTo(com.mapbox.maps.CameraOptions cameraOptions, com.mapbox.maps.plugin.animation.MapAnimationOptions? animationOptions = null, com.mapbox.maps.plugin.viewport.CompletionListener? completionListener = null);
method public com.mapbox.maps.CameraState? getCameraState();
Expand Down
2 changes: 2 additions & 0 deletions extension-compose/api/extension-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ public final class com/mapbox/maps/extension/compose/animation/viewport/MapViewp
public fun <init> ()V
public fun <init> (Lcom/mapbox/maps/CameraState;)V
public synthetic fun <init> (Lcom/mapbox/maps/CameraState;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun cameraForCoordinates (Ljava/util/List;Lcom/mapbox/maps/CameraOptions;Lcom/mapbox/maps/EdgeInsets;Ljava/lang/Double;Lcom/mapbox/maps/ScreenCoordinate;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun cameraForCoordinates$default (Lcom/mapbox/maps/extension/compose/animation/viewport/MapViewportState;Ljava/util/List;Lcom/mapbox/maps/CameraOptions;Lcom/mapbox/maps/EdgeInsets;Ljava/lang/Double;Lcom/mapbox/maps/ScreenCoordinate;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public final fun easeTo (Lcom/mapbox/maps/CameraOptions;Lcom/mapbox/maps/plugin/animation/MapAnimationOptions;Lcom/mapbox/maps/plugin/viewport/CompletionListener;)V
public static synthetic fun easeTo$default (Lcom/mapbox/maps/extension/compose/animation/viewport/MapViewportState;Lcom/mapbox/maps/CameraOptions;Lcom/mapbox/maps/plugin/animation/MapAnimationOptions;Lcom/mapbox/maps/plugin/viewport/CompletionListener;ILjava/lang/Object;)V
public final fun flyTo (Lcom/mapbox/maps/CameraOptions;Lcom/mapbox/maps/plugin/animation/MapAnimationOptions;Lcom/mapbox/maps/plugin/viewport/CompletionListener;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.mapbox.maps.CameraState
import com.mapbox.maps.EdgeInsets
import com.mapbox.maps.MapView
import com.mapbox.maps.MapboxMap
import com.mapbox.maps.ScreenCoordinate
import com.mapbox.maps.coroutine.cameraChangedEvents
import com.mapbox.maps.dsl.cameraOptions
import com.mapbox.maps.plugin.animation.CameraAnimationsPlugin
Expand All @@ -40,6 +41,8 @@ import com.mapbox.maps.plugin.viewport.transition.ViewportTransition
import com.mapbox.maps.plugin.viewport.viewport
import com.mapbox.maps.toCameraOptions
import kotlinx.coroutines.channels.Channel
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

/**
* Create and [rememberSaveable] a [MapViewportState] using [MapViewportState.Saver].
Expand Down Expand Up @@ -180,6 +183,38 @@ public class MapViewportState(
setCameraOptions(cameraOptions(block))
}

/**
* Convenience method that returns the [CameraOptions] object for given parameters.
*
* @param coordinates The `coordinates` representing the bounds of the camera.
* @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.
* @param coordinatesPadding The amount of padding in pixels to add to the given `coordinates`.
* 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`.
* @param maxZoom The maximum zoom level allowed in the returned camera options.
* @param offset The center of the given bounds relative to map center in pixels.
*
* @return The [CameraOptions] object representing the provided parameters.
*/
@UiThread
public suspend fun cameraForCoordinates(
coordinates: List<Point>,
camera: CameraOptions = cameraOptions { },
coordinatesPadding: EdgeInsets? = null,
maxZoom: Double? = null,
offset: ScreenCoordinate? = null,
): CameraOptions = suspendCoroutine { continuation ->
viewportActionChannel.trySend { mapView ->
mapView.mapboxMap.cameraForCoordinates(
coordinates,
camera,
coordinatesPadding,
maxZoom,
offset,
continuation::resume
)
}
}

/**
* A utility function to get the default [CameraOptions] defined in the style or null if this
* [MapViewportState] is not yet associated with a map.
Expand Down

0 comments on commit cf57218

Please sign in to comment.