-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add images to the style (#168)
- Loading branch information
Showing
15 changed files
with
279 additions
and
6 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
demo-app/src/commonMain/composeResources/drawable/marker.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="20dp" | ||
android:height="56dp" | ||
android:viewportWidth="20" | ||
android:viewportHeight="56"> | ||
<path | ||
android:pathData="M1,27a9,5 0,1 0,18 0a9,5 0,1 0,-18 0z" | ||
android:strokeAlpha="0.2" | ||
android:fillColor="#262626" | ||
android:fillAlpha="0.2"/> | ||
<path | ||
android:pathData="M19.5,10.4c0,6.3 -9.5,17.1 -9.5,17.1S0.5,16.6 0.5,10.4c0,-5.5 4.3,-9.9 9.5,-9.9S19.5,4.9 19.5,10.4z" | ||
android:strokeLineJoin="round" | ||
android:strokeWidth="1.0229" | ||
android:fillColor="#F84D4D" | ||
android:strokeColor="#951212" | ||
android:strokeLineCap="round"/> | ||
<path | ||
android:strokeWidth="1" | ||
android:pathData="M10,10m-3.8,0a3.8,3.8 0,1 1,7.6 0a3.8,3.8 0,1 1,-7.6 0" | ||
android:strokeLineJoin="round" | ||
android:fillColor="#FFFFFF" | ||
android:strokeColor="#7C2525" | ||
android:strokeLineCap="round"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
demo-app/src/commonMain/kotlin/dev/sargunv/maplibrecompose/demoapp/demos/MarkersDemo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package dev.sargunv.maplibrecompose.demoapp.demos | ||
|
||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material3.AlertDialog | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Canvas | ||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.drawscope.CanvasDrawScope | ||
import androidx.compose.ui.graphics.painter.Painter | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.platform.LocalLayoutDirection | ||
import dev.sargunv.maplibrecompose.compose.ClickResult | ||
import dev.sargunv.maplibrecompose.compose.MaplibreMap | ||
import dev.sargunv.maplibrecompose.compose.layer.SymbolLayer | ||
import dev.sargunv.maplibrecompose.compose.rememberCameraState | ||
import dev.sargunv.maplibrecompose.compose.rememberStyleState | ||
import dev.sargunv.maplibrecompose.compose.source.rememberGeoJsonSource | ||
import dev.sargunv.maplibrecompose.core.CameraPosition | ||
import dev.sargunv.maplibrecompose.core.expression.ExpressionsDsl.const | ||
import dev.sargunv.maplibrecompose.core.expression.ExpressionsDsl.image | ||
import dev.sargunv.maplibrecompose.demoapp.DEFAULT_STYLE | ||
import dev.sargunv.maplibrecompose.demoapp.Demo | ||
import dev.sargunv.maplibrecompose.demoapp.DemoMapControls | ||
import dev.sargunv.maplibrecompose.demoapp.DemoOrnamentSettings | ||
import dev.sargunv.maplibrecompose.demoapp.DemoScaffold | ||
import dev.sargunv.maplibrecompose.demoapp.generated.Res | ||
import dev.sargunv.maplibrecompose.demoapp.generated.marker | ||
import io.github.dellisd.spatialk.geojson.Feature | ||
import io.github.dellisd.spatialk.geojson.Position | ||
import org.jetbrains.compose.resources.painterResource | ||
|
||
@Composable | ||
private fun Painter.rememberAsBitmap(): ImageBitmap { | ||
val density = LocalDensity.current | ||
val layoutDirection = LocalLayoutDirection.current | ||
return remember(this, density, layoutDirection) { | ||
ImageBitmap(intrinsicSize.width.toInt(), intrinsicSize.height.toInt()).also { bitmap -> | ||
CanvasDrawScope().draw(density, layoutDirection, Canvas(bitmap), intrinsicSize) { draw(size) } | ||
} | ||
} | ||
} | ||
|
||
private val CHICAGO = Position(latitude = 41.878, longitude = -87.626) | ||
|
||
object MarkersDemo : Demo { | ||
override val name = "Markers" | ||
override val description = "Add and interact with markers" | ||
|
||
@Composable | ||
override fun Component(navigateUp: () -> Unit) { | ||
DemoScaffold(this, navigateUp) { | ||
val marker = painterResource(Res.drawable.marker).rememberAsBitmap() | ||
val cameraState = | ||
rememberCameraState(firstPosition = CameraPosition(target = CHICAGO, zoom = 7.0)) | ||
val styleState = rememberStyleState(images = mapOf("demo-marker" to marker)) | ||
var selectedFeature by remember { mutableStateOf<Feature?>(null) } | ||
|
||
Box(modifier = Modifier.fillMaxSize()) { | ||
MaplibreMap( | ||
styleUri = DEFAULT_STYLE, | ||
cameraState = cameraState, | ||
styleState = styleState, | ||
ornamentSettings = DemoOrnamentSettings(), | ||
) { | ||
val amtrakStations = | ||
rememberGeoJsonSource( | ||
id = "amtrak-stations", | ||
uri = | ||
"https://raw.githubusercontent.com/datanews/amtrak-geojson/refs/heads/master/amtrak-stations.geojson", | ||
) | ||
SymbolLayer( | ||
id = "amtrak-stations", | ||
source = amtrakStations, | ||
onClick = { features -> | ||
selectedFeature = features.firstOrNull() | ||
ClickResult.Consume | ||
}, | ||
iconImage = image(const("demo-marker")), | ||
iconAllowOverlap = const(true), | ||
) | ||
} | ||
DemoMapControls(cameraState, styleState) | ||
} | ||
|
||
selectedFeature?.let { feature -> | ||
AlertDialog( | ||
onDismissRequest = { selectedFeature = null }, | ||
confirmButton = {}, | ||
title = { Text(feature.getStringProperty("STNNAME") ?: "") }, | ||
text = { | ||
Column { | ||
Text("Station Code: ${feature.getStringProperty("STNCODE") ?: ""}") | ||
Text("Station Type: ${feature.getStringProperty("STNTYPE") ?: ""}") | ||
Text("Address: ${feature.getStringProperty("ADDRESS1") ?: ""}") | ||
Text("City: ${feature.getStringProperty("CITY") ?: ""}") | ||
Text("State: ${feature.getStringProperty("STATE") ?: ""}") | ||
Text("Zip: ${feature.getStringProperty("ZIP") ?: ""}") | ||
} | ||
}, | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
lib/maplibre-compose/src/androidMain/kotlin/dev/sargunv/maplibrecompose/core/Image.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.sargunv.maplibrecompose.core | ||
|
||
import android.graphics.Bitmap | ||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.asAndroidBitmap | ||
import androidx.compose.ui.unit.Density | ||
|
||
internal actual class Image(actual val id: String, val impl: Bitmap) { | ||
internal actual constructor( | ||
id: String, | ||
image: ImageBitmap, | ||
density: Density, | ||
) : this(id, image.asAndroidBitmap()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
lib/maplibre-compose/src/commonMain/kotlin/dev/sargunv/maplibrecompose/core/Image.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package dev.sargunv.maplibrecompose.core | ||
|
||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.unit.Density | ||
|
||
internal expect class Image { | ||
val id: String | ||
|
||
@Suppress("ConvertSecondaryConstructorToPrimary") | ||
internal constructor(id: String, image: ImageBitmap, density: Density) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 15 additions & 1 deletion
16
lib/maplibre-compose/src/commonTest/kotlin/dev/sargunv/maplibrecompose/compose/FakeStyle.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
lib/maplibre-compose/src/iosMain/kotlin/dev/sargunv/maplibrecompose/core/Image.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.sargunv.maplibrecompose.core | ||
|
||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.unit.Density | ||
import dev.sargunv.maplibrecompose.core.util.toUIImage | ||
import platform.UIKit.UIImage | ||
|
||
internal actual class Image(actual val id: String, val impl: UIImage) { | ||
internal actual constructor( | ||
id: String, | ||
image: ImageBitmap, | ||
density: Density, | ||
) : this(id, image.toUIImage(density)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.