如何在Android Jetpack Compose中禁用MapBox地图比例尺
问题:如何禁用MapBox地图上的比例尺?
我使用Android Jetpack Compose开发,MapBox SDK版本为11.7.1,Android Studio版本为Ladybug。尝试多种方法均未解决,且在MapBox官网未找到相关文档说明。
我的地图组件代码如下:
import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import com.mapbox.maps.extension.compose.MapEffect import com.mapbox.maps.extension.compose.MapboxMap import com.mapbox.maps.extension.compose.animation.viewport.rememberMapViewportState import com.mapbox.maps.plugin.PuckBearing import com.mapbox.maps.plugin.locationcomponent.createDefault2DPuck import com.mapbox.maps.plugin.locationcomponent.location class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { MapViewActivity() } } } @Composable fun MapViewActivity() { val mapViewportState = rememberMapViewportState() MapboxMap( Modifier.fillMaxSize(), mapViewportState = mapViewportState, ) { MapEffect(Unit) { mapView -> mapView.location.updateSettings { locationPuck = createDefault2DPuck(withBearing = true) pulsingEnabled = true enabled = true puckBearing = PuckBearing.COURSE puckBearingEnabled = true } mapViewportState.transitionToFollowPuckState() } } }
比例尺截图:
解决方法
MapBox Android SDK中,比例尺由scalebar插件提供,你可以在MapEffect代码块中获取该插件并禁用它。只需在现有逻辑中添加一行代码即可:
// 获取比例尺插件并禁用 mapView.scalebar.enabled = false
修改后的完整代码如下:
import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import com.mapbox.maps.extension.compose.MapEffect import com.mapbox.maps.extension.compose.MapboxMap import com.mapbox.maps.extension.compose.animation.viewport.rememberMapViewportState import com.mapbox.maps.plugin.PuckBearing import com.mapbox.maps.plugin.locationcomponent.createDefault2DPuck import com.mapbox.maps.plugin.locationcomponent.location class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { MapViewActivity() } } } @Composable fun MapViewActivity() { val mapViewportState = rememberMapViewportState() MapboxMap( Modifier.fillMaxSize(), mapViewportState = mapViewportState, ) { MapEffect(Unit) { mapView -> mapView.location.updateSettings { locationPuck = createDefault2DPuck(withBearing = true) pulsingEnabled = true enabled = true puckBearing = PuckBearing.COURSE puckBearingEnabled = true } // 新增:禁用比例尺 mapView.scalebar.enabled = false mapViewportState.transitionToFollowPuckState() } } }
说明
scalebar是MapBox默认启用的内置插件,直接修改其enabled属性即可控制显示/隐藏。- 该方案适配MapBox Android SDK 11.x版本,包括你使用的11.7.1。
内容的提问来源于stack exchange,提问作者Leo.roxy
相关产品推荐
相关产品推荐

