Skip to content

Commit d8a9cd3

Browse files
Mapbox Compose HTML for jsMain target, composite Gradle build settings.gradle.kts for the :compose-mapbox-library
1 parent da3cabe commit d8a9cd3

34 files changed

+7048
-9
lines changed

compose-mapbox-library/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# compose-mapbox-library (based on https://github.com/dellisd/compose-web-mapbox)
2+
3+
A Compose for Web wrapper of Mapbox.
4+
5+
## Usage
6+
7+
To create a map, call `rememberMapboxState()` to obtain a map state object that you can use to
8+
programmatically interact with the map. Then call the `MapboxMap()` composable and pass it your
9+
Mapbox access token and a map style URL at a minimum.
10+
11+
```kotlin
12+
val mapState = rememberMapboxState()
13+
14+
MapboxMap(
15+
accessToken = MAPBOX_ACCESS_TOKEN,
16+
style = "mapbox://styles/mapbox/dark-v10",
17+
state = mapState,
18+
) {
19+
// Place your sources and layers here
20+
}
21+
```
22+
23+
### Adding Sources
24+
25+
Data sources are added using Composable methods in the `sources` block of the `MapboxMap` function.
26+
Currently, only GeoJSON sources are implemented.
27+
28+
```kotlin
29+
import geojson.GeoJsonObject
30+
31+
MapboxMap(/* ... */) {
32+
GeoJsonSource(id = "test", data = data.unsafeCast<GeoJsonObject>()) {
33+
// Layers for this data source go here
34+
}
35+
}
36+
```
37+
38+
### Adding Layers
39+
40+
Layers are added within the scope of a data source. The layer is automatically set up to pull data
41+
from that source.
42+
Paint and layout properties can be applied to the layer.
43+
44+
```kotlin
45+
MapboxMap(/* ... */) {
46+
GeoJsonSource(/* ... */) {
47+
CircleLayer(id = "circles") {
48+
circleColor("#FF0000")
49+
circleRadius(10)
50+
}
51+
}
52+
}
53+
```
54+
55+
All the source, layer, and paint/layout property functions are composable functions meaning that
56+
their properties
57+
can be updated like any other composable function.
58+
59+
```kotlin
60+
var size by remember { mutableStateOf(1) }
61+
62+
MapboxMap(/* ... */) {
63+
GeoJsonSource(/* ... */) {
64+
CircleLayer(id = "circles") {
65+
circleColor("#FF0000")
66+
// The circle radius will automatically update on the map
67+
circleRadius(size)
68+
}
69+
}
70+
}
71+
Button(attrs = {
72+
onClick = { size++ }
73+
}) {
74+
Text("Increase Size")
75+
}
76+
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
plugins {
2+
alias(libs.plugins.kotlin.multiplatform)
3+
alias(libs.plugins.compose)
4+
}
5+
6+
group = "ca.derekellis.mapbox"
7+
version = "0.1.0-SNAPSHOT"
8+
9+
kotlin {
10+
js(IR) {
11+
browser {
12+
commonWebpackConfig {
13+
cssSupport {
14+
enabled.set(true)
15+
}
16+
}
17+
}
18+
}
19+
sourceSets {
20+
val commonMain by getting
21+
val commonTest by getting {
22+
dependencies {
23+
implementation(kotlin("test"))
24+
}
25+
}
26+
val jsMain by getting {
27+
dependencies {
28+
implementation(compose.html.core)
29+
implementation(compose.runtime)
30+
implementation(libs.kotlinx.coroutine.core)
31+
implementation(npm("mapbox-gl", libs.versions.mapbox.get()))
32+
}
33+
}
34+
val jsTest by getting
35+
}
36+
}

0 commit comments

Comments
 (0)