androidkotlinandroid-jetpack-composemapbox

How to get all four corner coordinates in MAP BOX using jetpack compose


I want to get all corner(topLeft, topRight, bottomLeft, bottomRight) coordinates (Lat, Lng) here is the code

      MapboxMap(
        modifier = Modifier.fillMaxSize(),
        mapInitOptionsFactory = { context ->
            MapInitOptions(
                context = context,
                styleUri = Style.STANDARD,
            )
        },
        mapViewportState = mapViewportState,
        compassSettings = compassSettings,
        scaleBarSettings = scaleBarSetting,
        gesturesSettings = mapBoxUiSettings,
        attributionSettings = AttributionSettings {
            enabled = false
        },
    ) {
        MapEffect(key1 = Unit) { mapView ->
            mapView.mapboxMap.subscribeMapIdle(mapIdleCallback = {
                Log.d("Tag", "Idle ${mapView.mapboxMap.cameraState.center}")
            })
        }
    } // : MapboxMap

When mapbox is idle mapIdleCallback is triggered now what are the methods I need to call to get the four coordinates. Any help would be appreciated.


Solution

  • Update; I was able to get coordinates after a little more code research

    MapboxMap(
            modifier = Modifier.fillMaxSize(),
            mapInitOptionsFactory = { context ->
                MapInitOptions(
                    context = context,
                    styleUri = Style.STANDARD,
                )
            },
            mapViewportState = mapViewportState,
            compassSettings = compassSettings,
            scaleBarSettings = scaleBarSetting,
            gesturesSettings = mapBoxUiSettings,
            attributionSettings = AttributionSettings {
                enabled = false
            },
        ) {
            MapEffect(key1 = Unit) { mapView ->
                mapView.mapboxMap.subscribeMapIdle(mapIdleCallback = {
                    val coordinateBounds =
                        mapView.mapboxMap.coordinateBoundsForCamera(mapView.mapboxMap.cameraState.toCameraOptions())
                    // Extract individual corner coordinates
                    val southwest = coordinateBounds.southwest
                    val northeast = coordinateBounds.northeast
                    // Calculate other corner coordinates
                    val northwest = Point.fromLngLat(southwest.longitude(), northeast.latitude())
                    val southeast = Point.fromLngLat(northeast.longitude(), southwest.latitude())
                    list = listOf(
                        southwest,
                        northwest,
                        northeast,
                        southeast,
                        southwest
                    )
                })
            }
        } // : MapboxMap
    

    The method I was looking for is mapView.mapboxMap.cameraState.toCameraOptions() which provided me with northEast and southWest coordinates I was then able to get other two coordinates by using basic map coordination techniques.