Skip to content

Commit 835d7d1

Browse files
github-actions[bot]klokantechsysadminsasaprodribaba
authored
RD-1377: Add PixelRatio related map option and Commands - Kotlin (#87)
Co-authored-by: klokantechsysadmin <[email protected]> Co-authored-by: sasaprodribaba <[email protected]>
1 parent 1197a8a commit 835d7d1

File tree

7 files changed

+111
-0
lines changed

7 files changed

+111
-0
lines changed
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.commands.navigation
8+
9+
import com.maptiler.maptilersdk.bridge.MTBridge
10+
import com.maptiler.maptilersdk.bridge.MTCommand
11+
12+
internal class GetPixelRatio : MTCommand {
13+
override val isPrimitiveReturnType: Boolean = true
14+
15+
override fun toJS(): String = "${MTBridge.MAP_OBJECT}.getPixelRatio();"
16+
}
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.commands.navigation
8+
9+
import com.maptiler.maptilersdk.bridge.MTBridge
10+
import com.maptiler.maptilersdk.bridge.MTCommand
11+
12+
internal data class SetPixelRatio(
13+
val pixelRatio: Double,
14+
) : MTCommand {
15+
override val isPrimitiveReturnType: Boolean = false
16+
17+
override fun toJS(): String = "${MTBridge.MAP_OBJECT}.setPixelRatio($pixelRatio);"
18+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ class MTMapOptions {
231231
var maxTileCacheZoomLevels: Double? = null
232232
private set
233233

234+
/**
235+
* Overrides the device pixel ratio used for rendering.
236+
*
237+
* Set this to values greater than `1.0` to force high-DPI rendering on low-density devices,
238+
* or lower values to favour performance over sharpness.
239+
*/
240+
var pixelRatio: Double? = null
241+
private set
242+
234243
/**
235244
* Boolean indicating whether the map's pitch control with drag to rotate interaction will be disabled.
236245
*/
@@ -493,6 +502,7 @@ class MTMapOptions {
493502
maptilerLogoIsVisible: Boolean? = null,
494503
maxTileCacheSize: Double? = null,
495504
maxTileCacheZoomLevels: Double? = null,
505+
pixelRatio: Double? = null,
496506
shouldPitchWithRotate: Boolean? = null,
497507
shouldRefreshExpiredTiles: Boolean? = null,
498508
shouldRenderWorldCopies: Boolean? = null,
@@ -539,6 +549,7 @@ class MTMapOptions {
539549
this.maptilerLogoIsVisible = maptilerLogoIsVisible
540550
this.maxTileCacheSize = maxTileCacheSize
541551
this.maxTileCacheZoomLevels = maxTileCacheZoomLevels
552+
this.pixelRatio = pixelRatio
542553
this.shouldPitchWithRotate = shouldPitchWithRotate
543554
this.shouldRefreshExpiredTiles = shouldRefreshExpiredTiles
544555
this.shouldRenderWorldCopies = shouldRenderWorldCopies

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,11 @@ class MTMapViewController(
382382

383383
override suspend fun getRenderWorldCopies(): Boolean = navigableWorker.getRenderWorldCopies()
384384

385+
/**
386+
* Returns the pixel ratio currently used to render the map.
387+
*/
388+
override suspend fun getPixelRatio(): Double = navigableWorker.getPixelRatio()
389+
385390
/**
386391
* Returns the elevation of the map's center point in meters above sea level.
387392
*/
@@ -416,6 +421,13 @@ class MTMapViewController(
416421
override fun setIsCenterClampedToGround(isCenterClampedToGround: Boolean) =
417422
navigableWorker.setIsCenterClampedToGround(isCenterClampedToGround)
418423

424+
/**
425+
* Overrides the pixel ratio used to render the map.
426+
*
427+
* @param pixelRatio Desired pixel ratio. Values above `1.0` improve sharpness, values below trade detail for performance.
428+
*/
429+
override fun setPixelRatio(pixelRatio: Double) = navigableWorker.setPixelRatio(pixelRatio)
430+
419431
/**
420432
* Sets the elevation of the map's center point, in meters above sea level.
421433
*

MapTilerSDK/src/main/java/com/maptiler/maptilersdk/map/workers/navigable/MTNavigable.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ interface MTNavigable {
116116
*/
117117
suspend fun getRenderWorldCopies(): Boolean
118118

119+
/**
120+
* Returns the pixel ratio currently used to render the map.
121+
*/
122+
suspend fun getPixelRatio(): Double
123+
119124
/**
120125
* Returns the elevation of the map's center point in meters above sea level.
121126
*/
@@ -147,6 +152,13 @@ interface MTNavigable {
147152
*/
148153
fun setIsCenterClampedToGround(isCenterClampedToGround: Boolean)
149154

155+
/**
156+
* Overrides the pixel ratio used to render the map.
157+
*
158+
* @param pixelRatio Desired pixel ratio. Values above `1.0` improve sharpness, values below trade detail for performance.
159+
*/
160+
fun setPixelRatio(pixelRatio: Double)
161+
150162
/**
151163
* Sets the elevation of the map's center point, in meters above sea level.
152164
*

MapTilerSDK/src/main/java/com/maptiler/maptilersdk/map/workers/navigable/NavigableWorker.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import com.maptiler.maptilersdk.commands.navigation.GetCenterElevation
1919
import com.maptiler.maptilersdk.commands.navigation.GetMaxPitch
2020
import com.maptiler.maptilersdk.commands.navigation.GetMinPitch
2121
import com.maptiler.maptilersdk.commands.navigation.GetPitch
22+
import com.maptiler.maptilersdk.commands.navigation.GetPixelRatio
2223
import com.maptiler.maptilersdk.commands.navigation.GetRenderWorldCopies
2324
import com.maptiler.maptilersdk.commands.navigation.GetRoll
2425
import com.maptiler.maptilersdk.commands.navigation.JumpTo
@@ -31,6 +32,7 @@ import com.maptiler.maptilersdk.commands.navigation.SetCenterClampedToGround
3132
import com.maptiler.maptilersdk.commands.navigation.SetCenterElevation
3233
import com.maptiler.maptilersdk.commands.navigation.SetPadding
3334
import com.maptiler.maptilersdk.commands.navigation.SetPitch
35+
import com.maptiler.maptilersdk.commands.navigation.SetPixelRatio
3436
import com.maptiler.maptilersdk.commands.navigation.SetRoll
3537
import com.maptiler.maptilersdk.helpers.JsonConfig
3638
import com.maptiler.maptilersdk.map.LngLat
@@ -233,6 +235,19 @@ internal class NavigableWorker(
233235
}
234236
}
235237

238+
override suspend fun getPixelRatio(): Double {
239+
val returnTypeValue =
240+
bridge.execute(
241+
GetPixelRatio(),
242+
)
243+
244+
return when (returnTypeValue) {
245+
is DoubleValue -> returnTypeValue.value
246+
is StringValue -> returnTypeValue.value.toDoubleOrNull() ?: 0.0
247+
else -> 0.0
248+
}
249+
}
250+
236251
override suspend fun getCenterElevation(): Double {
237252
val returnTypeValue =
238253
bridge.execute(
@@ -274,6 +289,21 @@ internal class NavigableWorker(
274289
}
275290
}
276291

292+
override fun setPixelRatio(pixelRatio: Double) {
293+
val sanitized =
294+
if (pixelRatio.isFinite()) {
295+
pixelRatio.coerceAtLeast(0.0)
296+
} else {
297+
0.0
298+
}
299+
300+
scope.launch {
301+
bridge.execute(
302+
SetPixelRatio(sanitized),
303+
)
304+
}
305+
}
306+
277307
override fun setCenterElevation(elevation: Double) {
278308
scope.launch {
279309
bridge.execute(

MapTilerSDK/src/test/java/com/maptiler/maptilersdk/NavigationTests.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ import com.maptiler.maptilersdk.commands.navigation.GetMaxPitch
1313
import com.maptiler.maptilersdk.commands.navigation.GetMaxZoom
1414
import com.maptiler.maptilersdk.commands.navigation.GetMinPitch
1515
import com.maptiler.maptilersdk.commands.navigation.GetMinZoom
16+
import com.maptiler.maptilersdk.commands.navigation.GetPixelRatio
1617
import com.maptiler.maptilersdk.commands.navigation.GetRenderWorldCopies
1718
import com.maptiler.maptilersdk.commands.navigation.GetZoom
1819
import com.maptiler.maptilersdk.commands.navigation.PanBy
1920
import com.maptiler.maptilersdk.commands.navigation.PanTo
2021
import com.maptiler.maptilersdk.commands.navigation.Project
2122
import com.maptiler.maptilersdk.commands.navigation.SetMaxZoom
2223
import com.maptiler.maptilersdk.commands.navigation.SetMinZoom
24+
import com.maptiler.maptilersdk.commands.navigation.SetPixelRatio
2325
import com.maptiler.maptilersdk.commands.navigation.SetZoom
2426
import com.maptiler.maptilersdk.commands.navigation.ZoomIn
2527
import com.maptiler.maptilersdk.commands.navigation.ZoomOut
@@ -39,6 +41,11 @@ class NavigationTests {
3941
assertEquals("${MTBridge.MAP_OBJECT}.getRenderWorldCopies();", command.toJS())
4042
}
4143

44+
@Test fun getPixelRatioToJS_ReturnsCorrectJSString() {
45+
val command = GetPixelRatio()
46+
assertEquals("${MTBridge.MAP_OBJECT}.getPixelRatio();", command.toJS())
47+
}
48+
4249
@Test fun getCenterElevationToJS_ReturnsCorrectJSString() {
4350
val command = GetCenterElevation()
4451
assertEquals("${MTBridge.MAP_OBJECT}.getCenterElevation();", command.toJS())
@@ -94,6 +101,11 @@ class NavigationTests {
94101
assertEquals("${MTBridge.MAP_OBJECT}.setMinZoom(1.0);", command.toJS())
95102
}
96103

104+
@Test fun setPixelRatioToJS_ReturnsCorrectJSString() {
105+
val command = SetPixelRatio(2.5)
106+
assertEquals("${MTBridge.MAP_OBJECT}.setPixelRatio(2.5);", command.toJS())
107+
}
108+
97109
@Test fun panByToJS_ReturnsCorrectJSString() {
98110
val offset = MTPoint(1.0, 1.0)
99111
val command = PanBy(offset)

0 commit comments

Comments
 (0)