メインコンテンツまでスキップ

Change the map’s style

This example demonstrates how to change the map style at runtime using the Mapbox Maps SDK for Android.

The code below renders UI buttons which change the style using the loadStyle() method. The available styles are MAPBOX_STREETS, LIGHT, DARK, SATELLITE_STREETS, SATELLITE and OUTDOORS.

Android Examples App Available

This example code is part of the Maps SDK for Android Examples App, a working Android project available on GitHub. Android developers are encouraged to run the examples app locally to interact with this example in an emulator and explore other features of the Maps SDK.

See our Run the Maps SDK for Android Examples App tutorial for step-by-step instructions.

StyleSwitchActivity.kt
package com.mapbox.maps.testapp.examples

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.maps.MapboxMap
import com.mapbox.maps.Style
import com.mapbox.maps.testapp.databinding.ActivityStyleSwitchBinding

/**
* Example of changing style for a map in runtime.
*/
class StyleSwitchActivity : AppCompatActivity() {

private lateinit var mapboxMap: MapboxMap
private lateinit var binding: ActivityStyleSwitchBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityStyleSwitchBinding.inflate(layoutInflater)
setContentView(binding.root)

mapboxMap = binding.mapView.mapboxMap

// Instead of this you can add your default style to the map layout with xml attribute `app:mapbox_styleUri="mapbox://styles/streets-v12"`
mapboxMap.loadStyle(Style.MAPBOX_STREETS)

binding.streetsButton.setOnClickListener {
mapboxMap.loadStyle(Style.MAPBOX_STREETS)
}
binding.lightButton.setOnClickListener {
mapboxMap.loadStyle(Style.LIGHT)
}
binding.darkButton.setOnClickListener {
mapboxMap.loadStyle(Style.DARK)
}
binding.satelliteStreetsButton.setOnClickListener {
mapboxMap.loadStyle(Style.SATELLITE_STREETS)
}
binding.satelliteButton.setOnClickListener {
mapboxMap.loadStyle(Style.SATELLITE)
}
binding.outdoorsButton.setOnClickListener {
mapboxMap.loadStyle(Style.OUTDOORS)
}
binding.standardButton.setOnClickListener {
mapboxMap.loadStyle(Style.STANDARD)
}
binding.standardSatelliteButton.setOnClickListener {
mapboxMap.loadStyle(Style.STANDARD_SATELLITE)
}
}
}
この{Type}は役に立ちましたか?