Official integration patterns for Mapbox Maps SDK on Android. Covers installation, adding markers, user location, custom data, styles, camera control, and featureset interactions. Based on official Mapbox documentation.
在 Android 上使用 Kotlin、Jetpack Compose 和 View 系统集成 Mapbox Maps SDK v11 的官方模式。
使用此技能的场景:
官方资源:
创建 app/res/values/mapboxaccesstoken.xml:
xml
tools:ignore=UnusedResources>YOURMAPBOXACCESS_TOKEN
获取令牌: 登录 mapbox.com
在 settings.gradle.kts 中:
kotlin
dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven {
url = uri(https://api.mapbox.com/downloads/v2/releases/maven)
}
}
}
在模块 build.gradle.kts 中:
kotlin
android {
defaultConfig {
minSdk = 21
}
}
dependencies {
implementation(com.mapbox.maps:android:11.18.1)
}
对于 Jetpack Compose:
kotlin
dependencies {
implementation(com.mapbox.maps:android:11.18.1)
implementation(com.mapbox.extension:maps-compose:11.18.1)
}
基础地图:
kotlin
import androidx.compose.runtime.*
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
import com.mapbox.maps.extension.compose.*
import com.mapbox.maps.Style
import com.mapbox.geojson.Point
@Composable
fun MapScreen() {
MapboxMap(
modifier = Modifier.fillMaxSize()
) {
// 通过 MapEffect 初始化摄像头(默认加载 Style.STANDARD)
MapEffect(Unit) { mapView ->
// 设置初始摄像头位置
mapView.mapboxMap.setCamera(
CameraOptions.Builder()
.center(Point.fromLngLat(-122.4194, 37.7749))
.zoom(12.0)
.build()
)
}
}
}
带装饰元素:
kotlin
MapboxMap(
modifier = Modifier.fillMaxSize(),
scaleBar = {
ScaleBar(
enabled = true,
position = Alignment.BottomStart
)
},
compass = {
Compass(enabled = true)
}
) {
// 默认加载 Style.STANDARD
}
布局 XML(activity_map.xml):
xml
android:layoutwidth=matchparent
android:layoutheight=matchparent>
android:layoutwidth=matchparent
android:layoutheight=matchparent />
Activity:
kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.maps.MapView
import com.mapbox.maps.Style
import com.mapbox.geojson.Point
class MapActivity : AppCompatActivity() {
private lateinit var mapView: MapView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_map)
mapView = findViewById(R.id.mapView)
mapView.mapboxMap.setCamera(
CameraOptions.Builder()
.center(Point.fromLngLat(-122.4194, 37.7749))
.zoom(12.0)
.build()
)
mapView.mapboxMap.loadStyle(Style.STANDARD)
}
override fun onStart() {
super.onStart()
mapView.onStart()
}
override fun onStop() {
super.onStop()
mapView.onStop()
}
override fun onDestroy() {
super.onDestroy()
mapView.onDestroy()
}
}
点注释是标记地图位置最常用的方式。
Jetpack Compose:
kotlin
MapboxMap(modifier = Modifier.fillMaxSize()) {
MapEffect(Unit) { mapView ->
// 先加载样式
mapView.mapboxMap.loadStyle(Style.STANDARD)
// 创建注释管理器并添加标记
val annotationManager = mapView.annotations.createPointAnnotationManager()
val pointAnnotation = PointAnnotationOptions()
.withPoint(Point.fromLngLat(-122.4194, 37.7749))
.withIconImage(custom-marker)
annotationManager.create(pointAnnotation)
}
}
// 注意:Compose 没有声明式的 PointAnnotation 组件
// 必须通过 MapEffect 以命令式方式添加标记
View 系统:
kotlin
// 创建注释管理器(创建一次,后续复用)
val pointAnnotationManager = mapView.annotations.createPointAnnotationManager()
// 创建标记
val pointAnnotation = PointAnnotationOptions()
.withPoint(Point.fromLngLat(-122.4194, 37.7749))
.withIconImage(custom-marker)
pointAnnotationManager.create(pointAnnotation)
多个标记:
kotlin
val locations = listOf(
Point.fromLngLat(-122.4194, 37.7749),
Point.fromLngLat(-122.4094, 37.7849),
Point.fromLngLat(-122.4294, 37.7649)
)
val annotations = locations.map { point ->
PointAnnotationOptions()
.withPoint(point)
.withIconImage(marker)
}
pointAnnotationManager.create(annotations)
第 1 步:在 AndroidManifest.xml 中添加权限:
xml
第 2 步:请求权限并显示位置:
kotlin
// 先请求权限(使用 ActivityResultContracts)
// 显示位置图标
mapView.location.updateSettings {
enabled = true
puckBearingEnabled = true
}
kotlin
// 不要重复创建新的管理器
// val manager = mapView.annotations.createPointAnnotationManager() // 每次调用
// 创建一次,后续复用
val pointAnnotationManager = mapView.annotations.createPointAnnotationManager()
fun updateMarkers() {
pointAnnotationManager.deleteAll()
pointAnnotationManager.create(markers)
}
kotlin
// 一次性创建所有注释
pointAnnotationManager.create(allAnnotations)
// 不要在循环中逐个创建
kotlin
// 始终调用生命周期方法
override fun onStart() {
super.onStart()
mapView.onStart()
}
override fun onStop() {
super.onStop()
mapView.onStop()
}
override fun onDestroy() {
super.onDestroy()
mapView.onDestroy()
}
kotlin
// 标准样式经过优化,推荐使用
Style.STANDARD
// 仅在特定用例需要时使用其他样式
Style.STANDARD_SATELLITE // 卫星影像
检查:
kotlin
mapView.mapboxMap.subscribeStyleLoaded { _ ->
Log.d(Map, 样式加载成功)
// 在此处
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 mapbox-android-patterns-1775930175 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 mapbox-android-patterns-1775930175 技能
skillhub install mapbox-android-patterns-1775930175
文件大小: 12.2 KB | 发布时间: 2026-4-12 10:30