I'm using Google Map to show a map in my compose s...
# compose-android
y
I'm using Google Map to show a map in my compose screen, I have to show user's location and I use the Map Settings property, I set isMyLoacation = true. But the user location isn't accurate. How do I fix that, is there anyone who faced this error.
a
Is it GoogleMap Compose that you use?
y
yeah
a
Haven't used that feature. However, what I do is use most recent location of the device and draw a marker with custom location marker at that
LatLng
. The only problem is when user moves location, there's no update to the location marker and thus if you click on go to location button (if implemented), the map will show the correct location but not with the location marker. Haven't spent time on solving this issue, though I think triggering recomposition of the map should do the job. Not sure if this would work for you. My app is not in production.
c
are you running on an emulator?
y
Not, On real device.
and @Alexandru Gheorghe, I have go location, it implemented but if I click this button location still not changed.
I think it depends on my location radius because my location shows a very high radius.
a
Could be. I don't use that feature so I really don't know. Sorry I can't help more.
y
I found my mistake, the problem depends on getting permission for location service, If you use ACCESS_COARCE_LOCATION permission, you do not get an accurate location, for the accurate location you have to use ACCESS_FINE_LOCATION permission.
👍🏻 1
👍 1
😇 2
Copy code
LaunchedEffect(key1 = Unit) {
    if (
        ContextCompat.checkSelfPermission(
            applicationContext,
            Manifest.permission.ACCESS_FINE_LOCATION
        ) != PackageManager.PERMISSION_GRANTED
    ) {
        launcher.launch(Manifest.permission.ACCESS_FINE_LOCATION)
    } else {
        fusedLocationClient.lastLocation.addOnSuccessListener {
            if (it != null) {
                viewModel.onAction(UiAction.SetUserLocation(it.latitude, it.longitude))
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                    geocoder.getFromLocation(it.latitude, it.longitude, 1) { list ->
                        viewModel.onAction(
                            UiAction.SetUserLocationName(list.firstOrNull()?.locality ?: "")
                        )
                    }
                } else {
                    val list = geocoder.getFromLocation(it.latitude, it.longitude, 1)
                    viewModel.onAction(
                        UiAction.SetUserLocationName(
                            list?.firstOrNull()?.locality ?: ""
                        )
                    )
                }
            } else {
                viewModel.onAction(UiAction.NavigateBack)
            }
        }
    }
}
I leave my code, it may be useful for someone🫡. Thanks everyone, happy coding!
thank you color 2