Skip to content

Commit 9d73696

Browse files
[create-pull-request] automated change (#90)
Co-authored-by: klokantechsysadmin <[email protected]>
1 parent 3626f92 commit 9d73696

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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.style
8+
9+
import com.maptiler.maptilersdk.bridge.MTBridge
10+
import com.maptiler.maptilersdk.bridge.MTCommand
11+
import com.maptiler.maptilersdk.helpers.formatUrlForJs
12+
import java.net.URL
13+
14+
internal data class AddSprite(
15+
val identifier: String,
16+
val spriteUrl: URL,
17+
) : MTCommand {
18+
override val isPrimitiveReturnType: Boolean = false
19+
20+
override fun toJS(): String {
21+
val sanitizedIdentifier = identifier.replace("\\", "\\\\").replace("'", "\\'")
22+
val formattedUrl = formatUrlForJs(spriteUrl)
23+
24+
return "${MTBridge.MAP_OBJECT}.addSprite('$sanitizedIdentifier', '$formattedUrl');"
25+
}
26+
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import com.maptiler.maptilersdk.map.workers.zoomable.ZoomableWorker
4141
import kotlinx.coroutines.CoroutineScope
4242
import kotlinx.coroutines.Dispatchers
4343
import kotlinx.coroutines.launch
44+
import java.net.URL
4445

4546
interface MTMapViewDelegate {
4647
fun onMapViewInitialized()
@@ -425,6 +426,19 @@ class MTMapViewController(
425426
*/
426427
suspend fun getProjection(): MTProjectionType = style?.getProjection() ?: MTProjectionType.MERCATOR
427428

429+
/**
430+
* Registers a sprite resource that can be referenced from style layers.
431+
*
432+
* @param identifier Unique identifier for the sprite.
433+
* @param url Fully qualified URL to the sprite image.
434+
*/
435+
fun addSprite(
436+
identifier: String,
437+
url: URL,
438+
) {
439+
style?.addSprite(identifier, url)
440+
}
441+
428442
/**
429443
* Project coordinates to point on the container.
430444
*/

MapTilerSDK/src/main/java/com/maptiler/maptilersdk/map/style/MTStyle.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ class MTStyle(
130130
options: MTAddImageOptions? = null,
131131
) = stylableWorker.addImage(identifier, image, options)
132132

133+
/**
134+
* Registers a sprite hosted at the provided URL under the given identifier.
135+
*
136+
* @param identifier Unique name for the sprite resource.
137+
* @param url Fully qualified URL pointing to the sprite image.
138+
*/
139+
fun addSprite(
140+
identifier: String,
141+
url: URL,
142+
) = stylableWorker.addSprite(identifier, url)
143+
133144
/**
134145
* Adds a layer to the map.
135146
*

MapTilerSDK/src/main/java/com/maptiler/maptilersdk/map/workers/stylable/StylableWorker.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import com.maptiler.maptilersdk.commands.misc.AddLogoControl
1919
import com.maptiler.maptilersdk.commands.style.AddImage
2020
import com.maptiler.maptilersdk.commands.style.AddLayer
2121
import com.maptiler.maptilersdk.commands.style.AddSource
22+
import com.maptiler.maptilersdk.commands.style.AddSprite
2223
import com.maptiler.maptilersdk.commands.style.DisableHalo
2324
import com.maptiler.maptilersdk.commands.style.DisableHaloAnimations
2425
import com.maptiler.maptilersdk.commands.style.DisableSpaceAnimations
@@ -215,6 +216,17 @@ internal class StylableWorker(
215216
}
216217
}
217218

219+
fun addSprite(
220+
identifier: String,
221+
spriteUrl: URL,
222+
) {
223+
scope.launch {
224+
bridge.execute(
225+
AddSprite(identifier, spriteUrl),
226+
)
227+
}
228+
}
229+
218230
fun removeLayer(layer: MTLayer) {
219231
scope.launch {
220232
bridge.execute(

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.maptiler.maptilersdk.bridge.MTCommand
1313
import com.maptiler.maptilersdk.bridge.MTCommandExecutable
1414
import com.maptiler.maptilersdk.commands.style.AddImage
1515
import com.maptiler.maptilersdk.commands.style.AddSource
16+
import com.maptiler.maptilersdk.commands.style.AddSprite
1617
import com.maptiler.maptilersdk.commands.style.DisableTerrain
1718
import com.maptiler.maptilersdk.commands.style.EnableGlobeProjection
1819
import com.maptiler.maptilersdk.commands.style.EnableMercatorProjection
@@ -210,6 +211,17 @@ class StyleAndCommandsTests {
210211
assertTrue(js.contains("\"pixelRatio\":2.0"))
211212
}
212213

214+
@Test fun addSpriteToJS_ReturnsValidJSString() {
215+
val url = URL("https://example.com/sprites/poi.png")
216+
217+
val js = AddSprite("poi-sprite", url).toJS()
218+
219+
assertEquals(
220+
"${MTBridge.MAP_OBJECT}.addSprite('poi-sprite', 'https://example.com/sprites/poi.png');",
221+
js,
222+
)
223+
}
224+
213225
@Test fun mtStyleAddImage_DelegatesToBridge() {
214226
val recordedCommands = mutableListOf<MTCommand>()
215227
val bridge =
@@ -231,4 +243,24 @@ class StyleAndCommandsTests {
231243
assertEquals(1, recordedCommands.size)
232244
assertTrue(recordedCommands.first() is AddImage)
233245
}
246+
247+
@Test fun mtStyleAddSprite_DelegatesToBridge() {
248+
val recordedCommands = mutableListOf<MTCommand>()
249+
val bridge =
250+
MTBridge(
251+
object : MTCommandExecutable {
252+
override suspend fun execute(command: MTCommand): MTBridgeReturnType {
253+
recordedCommands.add(command)
254+
return MTBridgeReturnType.Null
255+
}
256+
},
257+
)
258+
val style = MTStyle(MTMapReferenceStyle.STREETS)
259+
style.initWorker(bridge, CoroutineScope(Dispatchers.Unconfined))
260+
261+
style.addSprite("poi-sprite", URL("https://example.com/sprite.png"))
262+
263+
assertEquals(1, recordedCommands.size)
264+
assertTrue(recordedCommands.first() is AddSprite)
265+
}
234266
}

0 commit comments

Comments
 (0)