Skip to content

Commit ae4ccbc

Browse files
RD-980: Add panning functions
1 parent ddcb988 commit ae4ccbc

File tree

9 files changed

+173
-18
lines changed

9 files changed

+173
-18
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2025, MapTiler
3+
* All rights reserved.
4+
* SPDX-License-Identifier: BSD 3-Clause
5+
*/
6+
7+
package com.maptiler.maptilersdk.commands.navigation
8+
9+
import com.maptiler.maptilersdk.bridge.JSString
10+
import com.maptiler.maptilersdk.bridge.MTBridge
11+
import com.maptiler.maptilersdk.bridge.MTCommand
12+
import com.maptiler.maptilersdk.helpers.JsonConfig
13+
import com.maptiler.maptilersdk.map.types.MTPoint
14+
15+
internal data class PanBy(
16+
val offset: MTPoint,
17+
) : MTCommand {
18+
override fun toJS(): String {
19+
val offsetString: JSString = JsonConfig.json.encodeToString(offset)
20+
21+
return "${MTBridge.MAP_OBJECT}.panBy($offsetString);"
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2025, MapTiler
3+
* All rights reserved.
4+
* SPDX-License-Identifier: BSD 3-Clause
5+
*/
6+
7+
package com.maptiler.maptilersdk.commands.navigation
8+
9+
import com.maptiler.maptilersdk.bridge.JSString
10+
import com.maptiler.maptilersdk.bridge.MTBridge
11+
import com.maptiler.maptilersdk.bridge.MTCommand
12+
import com.maptiler.maptilersdk.helpers.JsonConfig
13+
import com.maptiler.maptilersdk.map.LngLat
14+
15+
internal data class PanTo(
16+
val coordinates: LngLat,
17+
) : MTCommand {
18+
override fun toJS(): String {
19+
val coordinatesString: JSString = JsonConfig.json.encodeToString(coordinates)
20+
21+
return "${MTBridge.MAP_OBJECT}.panTo($coordinatesString);"
22+
}
23+
}

MapTilerSDK/src/main/java/com/maptiler/maptilersdk/map/MTMapCameraHelper.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ import kotlinx.serialization.Serializable
1212
* A geographical location which contains a latitude, longitude pair.
1313
*/
1414
@Serializable
15-
data class LatLng(
16-
val lon: Double,
15+
data class LngLat(
16+
val lng: Double,
1717
val lat: Double,
1818
)
1919

2020
/**
2121
* Sets combination of center, bearing and pitch, as well as roll and elevation.
2222
*/
2323
class MTMapCameraHelper private constructor(
24-
val centerCoordinate: LatLng? = null,
24+
val centerCoordinate: LngLat? = null,
2525
val bearing: Double? = null,
2626
val pitch: Double? = null,
2727
val roll: Double? = null,
@@ -33,7 +33,7 @@ class MTMapCameraHelper private constructor(
3333
*/
3434
fun getCamera(): MTMapCameraHelper =
3535
MTMapCameraHelper(
36-
centerCoordinate = LatLng(0.0, 0.0),
36+
centerCoordinate = LngLat(0.0, 0.0),
3737
bearing = 0.0,
3838
pitch = 0.0,
3939
roll = 0.0,
@@ -51,7 +51,7 @@ class MTMapCameraHelper private constructor(
5151
* Returns camera object with the given center coordinate, bearing, pitch, roll and elevation.
5252
*/
5353
fun cameraLookingAtCenterCoordinate(
54-
centerCoordinate: LatLng,
54+
centerCoordinate: LngLat,
5555
bearing: Double,
5656
pitch: Double,
5757
roll: Double,
@@ -72,7 +72,7 @@ class MTMapCameraHelper private constructor(
7272
* If they are not specified in the style, they will default to 0.
7373
*/
7474
fun cameraLookingAtCenterCoordinate(
75-
centerCoordinate: LatLng,
75+
centerCoordinate: LngLat,
7676
bearing: Double,
7777
pitch: Double,
7878
): MTMapCameraHelper =

MapTilerSDK/src/main/java/com/maptiler/maptilersdk/map/MTMapOptions.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class MTMapOptions {
3131
* If center is not specified, SDK will look for it in the map style object.
3232
* If it is not specified in the style, it will default to (latitude: 0.0, longitude: 0.0).
3333
*/
34-
var center: LatLng? = null
34+
var center: LngLat? = null
3535
private set
3636

3737
/**
@@ -350,36 +350,36 @@ class MTMapOptions {
350350
private set
351351

352352
/** Initializes map options with center, zoom, bearing, and pitch. */
353-
constructor(center: LatLng?, zoom: Double?, bearing: Double?, pitch: Double?) {
353+
constructor(center: LngLat?, zoom: Double?, bearing: Double?, pitch: Double?) {
354354
this.center = center
355355
this.zoom = zoom
356356
this.bearing = bearing
357357
this.pitch = pitch
358358
}
359359

360360
/** Initializes map options with center and zoom. */
361-
constructor(center: LatLng?, zoom: Double?) {
361+
constructor(center: LngLat?, zoom: Double?) {
362362
this.center = center
363363
this.zoom = zoom
364364
}
365365

366366
/** Initializes map options with center, zoom and language. */
367-
constructor(center: LatLng?, zoom: Double?, language: MTLanguage) {
367+
constructor(center: LngLat?, zoom: Double?, language: MTLanguage) {
368368
this.center = center
369369
this.zoom = zoom
370370
this.language = language
371371
}
372372

373373
/** Initializes map options with center, zoom and terrain. */
374-
constructor(center: LatLng?, zoom: Double?, terrainIsEnabled: Boolean?, terrainExaggeration: Double?) {
374+
constructor(center: LngLat?, zoom: Double?, terrainIsEnabled: Boolean?, terrainExaggeration: Double?) {
375375
this.center = center
376376
this.zoom = zoom
377377
this.terrainIsEnabled = terrainIsEnabled
378378
this.terrainExaggeration = terrainExaggeration
379379
}
380380

381381
/** Initializes map options with center, zoom and projection. */
382-
constructor(center: LatLng?, zoom: Double?, projection: MTProjectionType?) {
382+
constructor(center: LngLat?, zoom: Double?, projection: MTProjectionType?) {
383383
this.center = center
384384
this.zoom = zoom
385385
this.projection = projection

MapTilerSDK/src/main/java/com/maptiler/maptilersdk/map/MTMapViewController.kt

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import com.maptiler.maptilersdk.logging.MTLogType
1717
import com.maptiler.maptilersdk.logging.MTLogger
1818
import com.maptiler.maptilersdk.map.gestures.MTGestureService
1919
import com.maptiler.maptilersdk.map.style.MTStyle
20+
import com.maptiler.maptilersdk.map.types.MTPoint
21+
import com.maptiler.maptilersdk.map.workers.navigable.MTNavigable
22+
import com.maptiler.maptilersdk.map.workers.navigable.NavigableWorker
2023
import com.maptiler.maptilersdk.map.workers.zoomable.MTZoomable
2124
import com.maptiler.maptilersdk.map.workers.zoomable.ZoomableWorker
2225
import kotlinx.coroutines.CoroutineScope
@@ -34,8 +37,9 @@ interface MTMapViewDelegate {
3437
class MTMapViewController(
3538
private val context: Context,
3639
) : WebViewExecutorDelegate,
37-
MTZoomable {
38-
var coroutineScope: CoroutineScope? = null
40+
MTZoomable,
41+
MTNavigable {
42+
private var coroutineScope: CoroutineScope? = null
3943

4044
private var bridge: MTBridge? = null
4145
private var webViewExecutor: WebViewExecutor? =
@@ -44,6 +48,7 @@ class MTMapViewController(
4448
}
4549

4650
private lateinit var zoomableWorker: ZoomableWorker
51+
private lateinit var navigableWorker: NavigableWorker
4752

4853
/**
4954
* Proxy style object of the map.
@@ -98,6 +103,7 @@ class MTMapViewController(
98103

99104
private fun initializeWorkers() {
100105
zoomableWorker = ZoomableWorker(bridge!!)
106+
navigableWorker = NavigableWorker(bridge!!)
101107
}
102108

103109
fun bind(scope: CoroutineScope) {
@@ -120,15 +126,55 @@ class MTMapViewController(
120126
}
121127
}
122128

129+
// ZOOMABLE
130+
131+
/**
132+
* Increases the map's zoom level by 1.
133+
*/
123134
override suspend fun zoomIn() = zoomableWorker.zoomIn()
124135

136+
/**
137+
* Decreases the map's zoom level by 1.
138+
*/
125139
override suspend fun zoomOut() = zoomableWorker.zoomOut()
126140

141+
/**
142+
* Returns the map's current zoom level.
143+
*/
127144
override suspend fun getZoom(): Double = zoomableWorker.getZoom()
128145

146+
/**
147+
* Sets the map's zoom level.
148+
*
149+
* @param zoom The zoom level to set (0-20).
150+
*/
129151
override suspend fun setZoom(zoom: Double) = zoomableWorker.setZoom(zoom)
130152

153+
/**
154+
* Sets the map's maximum zoom.
155+
* @param maxZoom Desired zoom.
156+
*/
131157
override suspend fun setMaxZoom(maxZoom: Double) = zoomableWorker.setMaxZoom(maxZoom)
132158

159+
/**
160+
* Sets the map's minimum zoom.
161+
* @param minZoom Desired zoom.
162+
*/
133163
override suspend fun setMinZoom(minZoom: Double) = zoomableWorker.setMinZoom(minZoom)
164+
165+
// NAVIGABLE
166+
167+
/**
168+
* Pans the map by the specified offset.
169+
*
170+
* @param offset Offset to pan by.
171+
*/
172+
override suspend fun panBy(offset: MTPoint) = navigableWorker.panBy(offset)
173+
174+
/**
175+
* Pans the map to the specified location with an animated transition.
176+
*
177+
* @param coordinates Coordinates to pan to.
178+
*/
179+
override suspend fun panTo(coordinates: LngLat) = navigableWorker.panTo(coordinates)
134180
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) 2025, MapTiler
3+
* All rights reserved.
4+
* SPDX-License-Identifier: BSD 3-Clause
5+
*/
6+
7+
package com.maptiler.maptilersdk.map.types
8+
9+
import kotlinx.serialization.Serializable
10+
11+
/**
12+
* Two numbers representing x and y screen coordinates in pixels.
13+
*/
14+
@Serializable
15+
data class MTPoint(
16+
val x: Double,
17+
val y: Double,
18+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) 2025, MapTiler
3+
* All rights reserved.
4+
* SPDX-License-Identifier: BSD 3-Clause
5+
*/
6+
7+
package com.maptiler.maptilersdk.map.workers.navigable
8+
9+
import com.maptiler.maptilersdk.map.LngLat
10+
import com.maptiler.maptilersdk.map.types.MTPoint
11+
12+
interface MTNavigable {
13+
suspend fun panBy(offset: MTPoint)
14+
15+
suspend fun panTo(coordinates: LngLat)
16+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2025, MapTiler
3+
* All rights reserved.
4+
* SPDX-License-Identifier: BSD 3-Clause
5+
*/
6+
7+
package com.maptiler.maptilersdk.map.workers.navigable
8+
9+
import com.maptiler.maptilersdk.bridge.MTBridge
10+
import com.maptiler.maptilersdk.commands.navigation.PanBy
11+
import com.maptiler.maptilersdk.commands.navigation.PanTo
12+
import com.maptiler.maptilersdk.map.LngLat
13+
import com.maptiler.maptilersdk.map.types.MTPoint
14+
15+
internal class NavigableWorker(
16+
private val bridge: MTBridge,
17+
) : MTNavigable {
18+
override suspend fun panBy(offset: MTPoint) {
19+
bridge.execute(
20+
PanBy(offset),
21+
)
22+
}
23+
24+
override suspend fun panTo(coordinates: LngLat) {
25+
bridge.execute(
26+
PanTo(coordinates),
27+
)
28+
}
29+
}

MapTilerSDK/src/main/java/com/maptiler/maptilersdk/map/workers/zoomable/ZoomableWorker.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ internal class ZoomableWorker(
3737
GetZoom(),
3838
)
3939

40-
when (returnTypeValue) {
41-
is StringValue -> return returnTypeValue.value.toDouble()
42-
is DoubleValue -> return returnTypeValue.value
43-
else -> return 0.0
40+
return when (returnTypeValue) {
41+
is StringValue -> returnTypeValue.value.toDouble()
42+
is DoubleValue -> returnTypeValue.value
43+
else -> 0.0
4444
}
4545
}
4646

0 commit comments

Comments
 (0)