Skip to content

Commit eb2ef60

Browse files
Update compose map to reposition google compass based on alignment setting
1 parent 2a959b9 commit eb2ef60

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ android.nonTransitiveRClass=true
2323
kotlin.code.style=official
2424
android.nonFinalResIds=false
2525
LIBRARY_VERSION=2.1.0
26-
LIBRARY_COMPOSE_VERSION=2.1.3
26+
LIBRARY_COMPOSE_VERSION=2.1.4
2727
MAPBOX_DOWNLOADS_TOKEN = "YOUR_MAPBOX_DOWNLOADS_TOKEN"

lib-compose/src/main/java/com/what3words/components/compose/maps/W3WMapComponent.kt

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.what3words.components.compose.maps
22

3-
import android.Manifest
43
import android.graphics.PointF
4+
import android.view.View
5+
import android.widget.RelativeLayout
56
import androidx.compose.foundation.layout.Box
67
import androidx.compose.foundation.layout.padding
78
import androidx.compose.runtime.Composable
@@ -18,8 +19,8 @@ import androidx.compose.ui.Modifier
1819
import androidx.compose.ui.geometry.Rect
1920
import androidx.compose.ui.layout.boundsInParent
2021
import androidx.compose.ui.layout.onGloballyPositioned
21-
import com.google.accompanist.permissions.ExperimentalPermissionsApi
22-
import com.google.accompanist.permissions.rememberMultiplePermissionsState
22+
import androidx.compose.ui.platform.LocalView
23+
import androidx.core.view.doOnLayout
2324
import com.what3words.components.compose.maps.W3WMapDefaults.MapColors
2425
import com.what3words.components.compose.maps.W3WMapDefaults.defaultMapColors
2526
import com.what3words.components.compose.maps.buttons.MapButtons
@@ -314,6 +315,8 @@ internal fun W3WMapContent(
314315
onMapViewPortProvided: (W3WGridScreenCell) -> Unit,
315316
onRecallButtonPositionProvided: ((PointF) -> Unit),
316317
) {
318+
val view = LocalView.current
319+
317320
var bounds by remember { mutableStateOf(Rect.Zero) }
318321

319322
// Check if the map is initialized, use to prevent LaunchedEffect to re-run on configuration changes
@@ -376,6 +379,24 @@ internal fun W3WMapContent(
376379
}
377380
}
378381

382+
// Reposition Google Map compass to align with app's design
383+
LaunchedEffect(mapProvider, mapConfig.isGoogleCompassAlignedRight) {
384+
if (mapProvider == MapProvider.GOOGLE_MAP && mapConfig.isGoogleCompassAlignedRight) {
385+
val compass = view.findViewWithTag<View>("GoogleMapCompass")
386+
387+
compass?.doOnLayout {
388+
val params = compass.layoutParams as RelativeLayout.LayoutParams
389+
params.addRule(RelativeLayout.ALIGN_PARENT_START, 0)
390+
params.addRule(RelativeLayout.ALIGN_PARENT_END)
391+
params.addRule(RelativeLayout.ALIGN_PARENT_TOP)
392+
393+
compass.layoutParams = params
394+
compass.requestLayout()
395+
}
396+
}
397+
}
398+
399+
379400
Box(
380401
modifier = modifier
381402
.onGloballyPositioned { coordinates ->
@@ -443,7 +464,7 @@ internal fun W3WMapView(
443464
onMarkerClicked: ((W3WMarker) -> Unit),
444465
onMapClicked: ((W3WCoordinates) -> Unit),
445466
onCameraUpdated: (W3WCameraState<*>) -> Unit,
446-
onMapProjectionUpdated: ((W3WMapProjection) -> Unit)? = null,
467+
onMapProjectionUpdated: ((W3WMapProjection) -> Unit)? = null,
447468
) {
448469
when (mapProvider) {
449470
MapProvider.GOOGLE_MAP -> {

lib-compose/src/main/java/com/what3words/components/compose/maps/W3WMapDefaults.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ object W3WMapDefaults {
7171
val darkModeCustomJsonStyle: String?,
7272
val isBuildingEnable: Boolean,
7373
val isCompassButtonEnabled: Boolean,
74+
val isGoogleCompassAlignedRight: Boolean,
7475
val isScaleBarEnabled: Boolean,
7576
val shouldFocusOnMyLocationOnInitialization: Boolean,
7677
val gridLineConfig: GridLinesConfig,
@@ -208,6 +209,7 @@ object W3WMapDefaults {
208209
darkModeCustomJsonStyle: String? = null,
209210
isBuildingEnable: Boolean = true,
210211
isCompassButtonEnabled: Boolean = true,
212+
isGoogleCompassAlignedRight: Boolean = true,
211213
isScaleBarEnabled: Boolean = false,
212214
shouldFocusOnMyLocationOnInitialization: Boolean = true,
213215
gridLineConfig: GridLinesConfig = defaultGridLinesConfig(),
@@ -220,6 +222,7 @@ object W3WMapDefaults {
220222
buttonConfig = buttonConfig,
221223
shouldFocusOnMyLocationOnInitialization = shouldFocusOnMyLocationOnInitialization,
222224
isCompassButtonEnabled = isCompassButtonEnabled,
225+
isGoogleCompassAlignedRight = isGoogleCompassAlignedRight,
223226
isScaleBarEnabled = isScaleBarEnabled
224227
)
225228
}

0 commit comments

Comments
 (0)