Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ package ovh.plrapps.mapcompose.demo.viewmodels

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import ovh.plrapps.mapcompose.api.addLayer
import ovh.plrapps.mapcompose.api.enableRotation
import ovh.plrapps.mapcompose.api.maxScale
import ovh.plrapps.mapcompose.api.minScale
import ovh.plrapps.mapcompose.api.onDoubleTap
import ovh.plrapps.mapcompose.api.scale
import ovh.plrapps.mapcompose.api.scrollTo
import ovh.plrapps.mapcompose.api.shouldLoopScale
import ovh.plrapps.mapcompose.demo.providers.makeTileStreamProvider
import ovh.plrapps.mapcompose.ui.state.MapState
Expand All @@ -17,5 +24,17 @@ class SimpleDemoVM(application: Application) : AndroidViewModel(application) {
addLayer(tileStreamProvider)
shouldLoopScale = true
enableRotation()
/**
* Example [onDoubleTap] callback that implements a different zoom behavior
*/
onDoubleTap { x, y ->
viewModelScope.launch {
if(scale < maxScale) {
scrollTo(x, y, maxScale)
} else {
scrollTo(x, y, minScale)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ fun MapState.onTap(tapCb: (x: Double, y: Double) -> Unit) {
this.tapCb = tapCb
}

/**
* Registers a callback for double-tap gestures. The callback is invoked with the relative
* coordinates of the double-tapped point on the map.
*
* When a callback is registered, it overrides the default double-tap zoom behavior.
*/
fun MapState.onDoubleTap(doubleTapCb: (x: Double, y: Double) -> Unit) {
this.doubleTapCb = doubleTapCb
}

/**
* Registers a callback for long press gestures. The callback is invoked with the relative coordinates
* of the pressed point on the map.
Expand All @@ -98,4 +108,4 @@ fun MapState.onLongPress(longPressCb: (x: Double, y: Double) -> Unit) {
*/
fun MapState.onTouchDown(cb: () -> Unit) {
touchDownCb = cb
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class MapState(
internal var stateChangeListener: (MapState.() -> Unit)? = null
internal var touchDownCb: (() -> Unit)? = null
internal var tapCb: LayoutTapCb? = null
internal var doubleTapCb: LayoutTapCb? = null
internal var longPressCb: LayoutTapCb? = null
internal var mapBackground by mutableStateOf(Color.Transparent)
internal var isFilteringBitmap: () -> Boolean by mutableStateOf(
Expand Down Expand Up @@ -133,6 +134,13 @@ class MapState(
tapCb?.invoke(x, y)
}

override fun onDoubleTap(x: Double, y: Double): Boolean {
return doubleTapCb?.let {
it(x, y)
true
} ?: false
}

override fun detectsTap(): Boolean = tapCb != null

override fun detectsLongPress(): Boolean = longPressCb != null
Expand Down Expand Up @@ -332,4 +340,4 @@ class InitialValues internal constructor() {
}
}

internal typealias LayoutTapCb = (x: Double, y: Double) -> Unit
internal typealias LayoutTapCb = (x: Double, y: Double) -> Unit
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,11 @@ internal class ZoomPanRotateState(
}

override fun onDoubleTap(focalPt: Offset) {
val isHandled = offsetToRelative(focalPt) { x, y ->
stateChangeListener.onDoubleTap(x, y)
}
if (isHandled) return

if (!isZoomingEnabled) return

val destScale = (
Expand Down Expand Up @@ -717,8 +722,9 @@ interface ZoomPanRotateStateListener {
fun onPress()
fun onLongPress(x: Double, y: Double)
fun onTap(x: Double, y: Double)
fun onDoubleTap(x: Double, y: Double): Boolean
fun detectsTap(): Boolean
fun detectsLongPress(): Boolean
fun interceptsTap(x: Double, y: Double, xPx: Int, yPx: Int): Boolean
fun interceptsLongPress(x: Double, y: Double, xPx: Int, yPx: Int): Boolean
}
}