Skip to content

Commit 9afec89

Browse files
Update google compass icon position (#143)
* Update compose map to reposition google compass based on alignment setting * Add docstring * Move logic to W3WGoogleMap
1 parent 2a959b9 commit 9afec89

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-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: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.what3words.components.compose.maps
22

3-
import android.Manifest
43
import android.graphics.PointF
54
import androidx.compose.foundation.layout.Box
65
import androidx.compose.foundation.layout.padding
@@ -18,8 +17,6 @@ import androidx.compose.ui.Modifier
1817
import androidx.compose.ui.geometry.Rect
1918
import androidx.compose.ui.layout.boundsInParent
2019
import androidx.compose.ui.layout.onGloballyPositioned
21-
import com.google.accompanist.permissions.ExperimentalPermissionsApi
22-
import com.google.accompanist.permissions.rememberMultiplePermissionsState
2320
import com.what3words.components.compose.maps.W3WMapDefaults.MapColors
2421
import com.what3words.components.compose.maps.W3WMapDefaults.defaultMapColors
2522
import com.what3words.components.compose.maps.buttons.MapButtons
@@ -443,7 +440,7 @@ internal fun W3WMapView(
443440
onMarkerClicked: ((W3WMarker) -> Unit),
444441
onMapClicked: ((W3WCoordinates) -> Unit),
445442
onCameraUpdated: (W3WCameraState<*>) -> Unit,
446-
onMapProjectionUpdated: ((W3WMapProjection) -> Unit)? = null,
443+
onMapProjectionUpdated: ((W3WMapProjection) -> Unit)? = null,
447444
) {
448445
when (mapProvider) {
449446
MapProvider.GOOGLE_MAP -> {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ object W3WMapDefaults {
6161
* @property darkModeCustomJsonStyle The custom JSON style for dark mode
6262
* @property isBuildingEnable Whether 3D buildings are enabled on the map
6363
* @property isCompassButtonEnabled Whether the compass button is enabled on the map
64+
* @property isGoogleCompassAlignedRight Whether the compass button should be aligned to the right on Google Maps
6465
* @property isScaleBarEnabled Whether the scale bar is enabled on the map
6566
* @property shouldFocusOnMyLocationOnInitialization Whether to automatically focus on user's location during map initialization
6667
* @property gridLineConfig The configuration for grid lines on the map
@@ -71,6 +72,7 @@ object W3WMapDefaults {
7172
val darkModeCustomJsonStyle: String?,
7273
val isBuildingEnable: Boolean,
7374
val isCompassButtonEnabled: Boolean,
75+
val isGoogleCompassAlignedRight: Boolean,
7476
val isScaleBarEnabled: Boolean,
7577
val shouldFocusOnMyLocationOnInitialization: Boolean,
7678
val gridLineConfig: GridLinesConfig,
@@ -208,6 +210,7 @@ object W3WMapDefaults {
208210
darkModeCustomJsonStyle: String? = null,
209211
isBuildingEnable: Boolean = true,
210212
isCompassButtonEnabled: Boolean = true,
213+
isGoogleCompassAlignedRight: Boolean = true,
211214
isScaleBarEnabled: Boolean = false,
212215
shouldFocusOnMyLocationOnInitialization: Boolean = true,
213216
gridLineConfig: GridLinesConfig = defaultGridLinesConfig(),
@@ -220,6 +223,7 @@ object W3WMapDefaults {
220223
buttonConfig = buttonConfig,
221224
shouldFocusOnMyLocationOnInitialization = shouldFocusOnMyLocationOnInitialization,
222225
isCompassButtonEnabled = isCompassButtonEnabled,
226+
isGoogleCompassAlignedRight = isGoogleCompassAlignedRight,
223227
isScaleBarEnabled = isScaleBarEnabled
224228
)
225229
}

lib-compose/src/main/java/com/what3words/components/compose/maps/providers/googlemap/W3WGoogleMap.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.what3words.components.compose.maps.providers.googlemap
22

33
import android.graphics.Point
4+
import android.view.View
5+
import android.widget.RelativeLayout
46
import androidx.compose.runtime.Composable
57
import androidx.compose.runtime.LaunchedEffect
68
import androidx.compose.runtime.getValue
@@ -9,6 +11,8 @@ import androidx.compose.runtime.remember
911
import androidx.compose.runtime.setValue
1012
import androidx.compose.runtime.snapshotFlow
1113
import androidx.compose.ui.Modifier
14+
import androidx.compose.ui.platform.LocalView
15+
import androidx.core.view.doOnLayout
1216
import com.google.android.gms.maps.Projection
1317
import com.google.android.gms.maps.model.LatLngBounds
1418
import com.google.android.gms.maps.model.MapStyleOptions
@@ -116,6 +120,24 @@ fun W3WGoogleMap(
116120
onCameraUpdated(state.cameraState)
117121
}
118122

123+
// Reposition Google Map compass to align with app's design
124+
val view = LocalView.current
125+
LaunchedEffect(mapConfig.isGoogleCompassAlignedRight) {
126+
if (mapConfig.isGoogleCompassAlignedRight) {
127+
val compass = view.findViewWithTag<View>("GoogleMapCompass")
128+
129+
compass?.doOnLayout {
130+
val params = compass.layoutParams as RelativeLayout.LayoutParams
131+
params.addRule(RelativeLayout.ALIGN_PARENT_START, 0)
132+
params.addRule(RelativeLayout.ALIGN_PARENT_END)
133+
params.addRule(RelativeLayout.ALIGN_PARENT_TOP)
134+
135+
compass.layoutParams = params
136+
compass.requestLayout()
137+
}
138+
}
139+
}
140+
119141
GoogleMap(
120142
modifier = modifier,
121143
cameraPositionState = cameraPositionState,

0 commit comments

Comments
 (0)