Skip to content

Commit 2d3c4e7

Browse files
committed
Add new sample for case existing map
1 parent e018574 commit 2d3c4e7

File tree

11 files changed

+614
-135
lines changed

11 files changed

+614
-135
lines changed

.github/workflows/BuildTest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
echo "MAPBOX_ACCESS_TOKEN=${MAPBOX_DOWNLOADS_TOKEN}">> ~/.gradle/gradle.properties
3636
echo "MAPS_API_KEY=${MAPBOX_DOWNLOADS_TOKEN}">> ~/.gradle/gradle.properties
3737
echo "PROD_API_KEY=${MAPBOX_DOWNLOADS_TOKEN}">> ~/.gradle/gradle.properties
38-
cat ~/.gradle/gradle.properties
38+
cat ~/.gradle/gradle.
3939
4040
- name: Set Up Gradle Wrapper
4141
run: ./gradlew wrapper

maps-googlemaps-sample/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ dependencies {
6969

7070
implementation "androidx.core:core-ktx:1.13.1"
7171

72-
implementation platform("androidx.compose:compose-bom:2024.06.00")
72+
implementation platform("androidx.compose:compose-bom:2025.03.00")
7373
implementation "androidx.activity:activity-compose:1.9.1"
7474
implementation "androidx.compose.material3:material3"
7575
implementation "androidx.compose.ui:ui"
@@ -78,6 +78,9 @@ dependencies {
7878
implementation "com.google.android.material:material:1.12.0"
7979
debugImplementation 'androidx.compose.ui:ui-tooling:1.7.5'
8080

81+
// Navigation Compose
82+
implementation("androidx.navigation:navigation-compose:2.8.9")
83+
8184
implementation "androidx.appcompat:appcompat:1.7.0"
8285
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.8.4"
8386
implementation "androidx.fragment:fragment-ktx:1.8.2"

maps-googlemaps-sample/src/main/java/com/what3words/samples/googlemaps/compose/MapComposeActivity.kt

Lines changed: 30 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -3,144 +3,49 @@ package com.what3words.samples.googlemaps.compose
33
import android.os.Bundle
44
import androidx.activity.ComponentActivity
55
import androidx.activity.compose.setContent
6-
import androidx.compose.foundation.layout.Box
7-
import androidx.compose.foundation.layout.fillMaxSize
8-
import androidx.compose.runtime.Composable
9-
import androidx.compose.runtime.DisposableEffect
10-
import androidx.compose.runtime.LaunchedEffect
11-
import androidx.compose.runtime.remember
12-
import androidx.compose.ui.Modifier
13-
import androidx.compose.ui.graphics.Color
14-
import androidx.compose.ui.platform.LocalContext
6+
import androidx.activity.viewModels
7+
import androidx.navigation.compose.NavHost
8+
import androidx.navigation.compose.composable
9+
import androidx.navigation.compose.rememberNavController
1510
import com.what3words.androidwrapper.datasource.text.W3WApiTextDataSource
16-
import com.what3words.components.compose.maps.MapProvider
17-
import com.what3words.components.compose.maps.W3WMapComponent
18-
import com.what3words.components.compose.maps.W3WMapDefaults
19-
import com.what3words.components.compose.maps.models.W3WMarkerColor
20-
import com.what3words.components.compose.maps.rememberW3WMapManager
21-
import com.what3words.core.datasource.text.W3WTextDataSource
22-
import com.what3words.core.types.geometry.W3WCoordinates
2311
import com.what3words.design.library.ui.theme.W3WTheme
2412
import com.what3words.samples.googlemaps.BuildConfig
25-
26-
val london1Coordinate = W3WCoordinates(51.513678, -0.133823)
27-
val london2Coordinate = W3WCoordinates(51.494947, -0.093923)
28-
val london3Coordinate = W3WCoordinates(51.473062, -0.142303)
29-
val london4Coordinate = W3WCoordinates(51.472335, -0.183234)
30-
val london5Coordinate = W3WCoordinates(51.471445, -0.239064)
31-
const val london1W3WAddress = "verbs.stacks.ranked"
32-
const val london2W3WAddress = "alarm.cargo.bills"
33-
const val london3W3WAddress = "gold.basin.freed"
34-
const val london4W3WAddress = "known.format.adults"
35-
const val london5W3WAddress = "piles.hedge.logo"
13+
import com.what3words.samples.googlemaps.compose.ui.home.HomeScreen
14+
import com.what3words.samples.googlemaps.compose.ui.overlaymap.ExistingMapWithDrawerScreen
15+
import com.what3words.samples.googlemaps.compose.ui.standalone.W3WMapComponentScreen
3616

3717
class MapComposeActivity : ComponentActivity() {
3818

3919
override fun onCreate(savedInstanceState: Bundle?) {
4020
super.onCreate(savedInstanceState)
4121

22+
val viewModel: MapViewModel by viewModels {
23+
MapViewModelFactory(
24+
textDataSource = W3WApiTextDataSource.create(this, BuildConfig.W3W_API_KEY)
25+
)
26+
}
27+
4228
setContent {
4329
W3WTheme {
44-
W3WMapComponentApp(
45-
textDataSource = W3WApiTextDataSource.create(this, BuildConfig.W3W_API_KEY)
46-
)
30+
val navController = rememberNavController()
31+
32+
NavHost(navController = navController, startDestination = "home") {
33+
composable("home") {
34+
HomeScreen(navController)
35+
}
36+
composable("first") {
37+
W3WMapComponentScreen(
38+
textDataSource = viewModel.w3WTextDataSource
39+
)
40+
}
41+
composable("second") {
42+
ExistingMapWithDrawerScreen(
43+
textDataSource = viewModel.w3WTextDataSource
44+
)
45+
}
46+
}
4747
}
4848
}
4949
}
5050
}
5151

52-
@Composable
53-
fun W3WMapComponentApp(
54-
modifier: Modifier = Modifier,
55-
textDataSource: W3WTextDataSource
56-
) {
57-
58-
val context = LocalContext.current
59-
60-
val mapManager = rememberW3WMapManager(
61-
mapProvider = MapProvider.GOOGLE_MAP
62-
)
63-
64-
val locationSource = remember {
65-
LocationSourceImpl(context)
66-
}
67-
68-
// Move the camera to a specific coordinate
69-
LaunchedEffect(Unit) {
70-
mapManager.moveToPosition(london1Coordinate)
71-
}
72-
73-
// Add a marker at a specific coordinate
74-
LaunchedEffect(Unit) {
75-
mapManager.addMarkerAt(
76-
coordinates = london2Coordinate,
77-
markerColor = W3WMarkerColor(background = Color.Red, slash = Color.Yellow)
78-
)
79-
}
80-
81-
// Add a list of marker at specific coordinates
82-
LaunchedEffect(Unit) {
83-
mapManager.addMarkersAt(
84-
listCoordinates = listOf(
85-
london3Coordinate,
86-
london4Coordinate,
87-
london5Coordinate
88-
),
89-
listName = "London",
90-
markerColor = W3WMarkerColor(background = Color.Yellow, slash = Color.Red)
91-
)
92-
}
93-
94-
// Select a specific address on the map
95-
LaunchedEffect(Unit) {
96-
mapManager.setSelectedAt(london1W3WAddress)
97-
}
98-
99-
// Add a marker at a specific what3words address
100-
LaunchedEffect(Unit) {
101-
mapManager.addMarkerAt(
102-
words = london2W3WAddress,
103-
markerColor = W3WMarkerColor(background = Color.Black, slash = Color.Yellow)
104-
)
105-
}
106-
107-
// Add a list of marker at specific coordinates
108-
LaunchedEffect(Unit) {
109-
mapManager.addMarkersAt(
110-
listWords = listOf(
111-
london3W3WAddress,
112-
london4W3WAddress,
113-
london5W3WAddress
114-
),
115-
listName = "London 2",
116-
markerColor = W3WMarkerColor(background = Color.Blue, slash = Color.White)
117-
)
118-
}
119-
120-
DisposableEffect(Unit) {
121-
onDispose {
122-
locationSource.onDestroy()
123-
}
124-
}
125-
126-
Box(
127-
modifier = modifier.fillMaxSize()
128-
) {
129-
W3WMapComponent(
130-
textDataSource = textDataSource,
131-
modifier = Modifier.fillMaxSize(),
132-
locationSource = locationSource,
133-
mapConfig = W3WMapDefaults.defaultMapConfig(
134-
buttonConfig = W3WMapDefaults.ButtonConfig(
135-
isRecallFeatureEnabled = true,
136-
isMapSwitchFeatureEnabled = true,
137-
isMyLocationFeatureEnabled = true
138-
)
139-
),
140-
mapManager = mapManager,
141-
onSelectedSquareChanged = {
142-
// Handle the selected square
143-
}
144-
)
145-
}
146-
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.what3words.samples.googlemaps.compose
2+
3+
import androidx.lifecycle.ViewModel
4+
import com.what3words.core.datasource.text.W3WTextDataSource
5+
6+
class MapViewModel(
7+
val w3WTextDataSource: W3WTextDataSource,
8+
) : ViewModel()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.what3words.samples.googlemaps.compose
2+
3+
import androidx.lifecycle.ViewModel
4+
import androidx.lifecycle.ViewModelProvider
5+
import com.what3words.core.datasource.text.W3WTextDataSource
6+
7+
class MapViewModelFactory(
8+
private val textDataSource: W3WTextDataSource,
9+
) : ViewModelProvider.Factory {
10+
override fun <T : ViewModel> create(modelClass: Class<T>): T {
11+
if (modelClass.isAssignableFrom(MapViewModel::class.java)) {
12+
return MapViewModel(
13+
textDataSource,
14+
) as T
15+
}
16+
throw IllegalArgumentException("Unknown ViewModel class")
17+
}
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.what3words.samples.googlemaps.compose.data
2+
3+
import com.what3words.core.types.geometry.W3WCoordinates
4+
5+
val london1Coordinate = W3WCoordinates(51.513678, -0.133823)
6+
val london2Coordinate = W3WCoordinates(51.494947, -0.093923)
7+
val london3Coordinate = W3WCoordinates(51.473062, -0.142303)
8+
val london4Coordinate = W3WCoordinates(51.472335, -0.183234)
9+
val london5Coordinate = W3WCoordinates(51.515106, -0.180303)
10+
const val london1W3WAddress = "verbs.stacks.ranked"
11+
const val london2W3WAddress = "alarm.cargo.bills"
12+
const val london3W3WAddress = "gold.basin.freed"
13+
const val london4W3WAddress = "known.format.adults"
14+
const val london5W3WAddress = "piles.hedge.logo"

maps-googlemaps-sample/src/main/java/com/what3words/samples/googlemaps/compose/LocationSourceImpl.kt renamed to maps-googlemaps-sample/src/main/java/com/what3words/samples/googlemaps/compose/data/LocationSourceImpl.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.what3words.samples.googlemaps.compose
1+
package com.what3words.samples.googlemaps.compose.data
22

33
import android.Manifest
44
import android.content.BroadcastReceiver
@@ -30,7 +30,6 @@ import kotlinx.coroutines.flow.distinctUntilChanged
3030
import kotlinx.coroutines.flow.launchIn
3131
import kotlinx.coroutines.flow.onEach
3232
import kotlin.coroutines.resume
33-
import kotlin.coroutines.resumeWithException
3433
import kotlin.coroutines.suspendCoroutine
3534

3635
class LocationSourceImpl(private val context: Context) : W3WLocationSource {
@@ -67,12 +66,13 @@ class LocationSourceImpl(private val context: Context) : W3WLocationSource {
6766

6867
try {
6968
return suspendCoroutine { cont ->
70-
fusedLocationClient.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, null).addOnCompleteListener { task ->
71-
if (task.isSuccessful && task.result != null) {
72-
_locationStatus.value = LocationStatus.ACTIVE
73-
cont.resume(task.result)
69+
fusedLocationClient.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, null)
70+
.addOnCompleteListener { task ->
71+
if (task.isSuccessful && task.result != null) {
72+
_locationStatus.value = LocationStatus.ACTIVE
73+
cont.resume(task.result)
74+
}
7475
}
75-
}
7676
}
7777
} catch (e: Exception) {
7878
_locationStatus.value = LocationStatus.INACTIVE
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.what3words.samples.googlemaps.compose.ui.home
2+
3+
import androidx.compose.foundation.layout.Arrangement
4+
import androidx.compose.foundation.layout.Column
5+
import androidx.compose.foundation.layout.fillMaxSize
6+
import androidx.compose.foundation.layout.fillMaxWidth
7+
import androidx.compose.foundation.layout.padding
8+
import androidx.compose.material3.Button
9+
import androidx.compose.material3.MaterialTheme
10+
import androidx.compose.material3.Text
11+
import androidx.compose.runtime.Composable
12+
import androidx.compose.ui.Alignment
13+
import androidx.compose.ui.Modifier
14+
import androidx.compose.ui.unit.dp
15+
import androidx.navigation.NavController
16+
17+
@Composable
18+
fun HomeScreen(navController: NavController) {
19+
Column(
20+
modifier = Modifier
21+
.fillMaxSize()
22+
.padding(16.dp),
23+
horizontalAlignment = Alignment.CenterHorizontally,
24+
verticalArrangement = Arrangement.spacedBy(16.dp, Alignment.CenterVertically),
25+
) {
26+
Text(
27+
text = "Welcome to What3Words Maps Demo",
28+
style = MaterialTheme.typography.headlineMedium
29+
)
30+
31+
Text(
32+
text = "This demo shows how to integrate What3Words with Google Maps in Compose.",
33+
style = MaterialTheme.typography.bodyLarge,
34+
modifier = Modifier.padding(bottom = 24.dp)
35+
)
36+
37+
Button(
38+
modifier = Modifier.fillMaxWidth().padding(16.dp),
39+
onClick = { navController.navigate("first") }) {
40+
Text("Using W3WMapComponent")
41+
}
42+
43+
44+
Button(
45+
modifier = Modifier.fillMaxWidth().padding(16.dp),
46+
onClick = { navController.navigate("second") }) {
47+
Text("Using W3WDrawer with Existing Map")
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)