|
| 1 | +package com.mapbox.mapboxandroiddemo.examples.styles |
| 2 | + |
| 3 | +import android.os.Bundle |
| 4 | +import android.widget.Toast |
| 5 | +import androidx.appcompat.app.AppCompatActivity |
| 6 | +import com.mapbox.mapboxandroiddemo.R |
| 7 | +import com.mapbox.mapboxsdk.Mapbox |
| 8 | +import com.mapbox.mapboxsdk.maps.MapView |
| 9 | +import com.mapbox.mapboxsdk.maps.Style |
| 10 | +import com.mapbox.mapboxsdk.style.expressions.Expression.* |
| 11 | +import com.mapbox.mapboxsdk.style.layers.LineLayer |
| 12 | +import kotlinx.android.synthetic.main.activity_style_worldview_switch.* |
| 13 | + |
| 14 | +/** |
| 15 | + * Uses the worldview value to adjust administrative boundaries. |
| 16 | + * You can see the worldview options within the worldviews variable in this example. |
| 17 | + * |
| 18 | + * More about Mapbox's worldview can be found at |
| 19 | + * https://docs.mapbox.com/vector-tiles/reference/mapbox-streets-v8/#-worldview-text |
| 20 | + */ |
| 21 | +class KotlinWorldviewSwitchActivity : AppCompatActivity() { |
| 22 | + |
| 23 | + private lateinit var mapView: MapView |
| 24 | + private var possibleWorldViews = mutableListOf("US", "CN", "IN") |
| 25 | + private var index = 0 |
| 26 | + |
| 27 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 28 | + super.onCreate(savedInstanceState) |
| 29 | + |
| 30 | + // Mapbox access token is configured here. This needs to be called either in your application |
| 31 | + // object or in the same activity which contains the mapview. |
| 32 | + Mapbox.getInstance(this, getString(R.string.access_token)) |
| 33 | + |
| 34 | + // This contains the MapView in XML and needs to be called after the access token is configured. |
| 35 | + setContentView(R.layout.activity_style_worldview_switch) |
| 36 | + mapView = findViewById(R.id.mapView) |
| 37 | + mapView.onCreate(savedInstanceState) |
| 38 | + mapView.getMapAsync { mapboxMap -> |
| 39 | + mapboxMap.setStyle(Style.MAPBOX_STREETS) { style -> |
| 40 | + |
| 41 | + switch_worldview_fab.setOnClickListener { |
| 42 | + |
| 43 | + style.layers.forEach { |
| 44 | + |
| 45 | + // Apply changes only to certain LineLayers in the style |
| 46 | + if (it.id == "admin-0-boundary" || |
| 47 | + it.id == "admin-1-boundary" || |
| 48 | + it.id == "admin-0-boundary-disputed" || |
| 49 | + it.id == "admin-1-boundary-bg" || |
| 50 | + it.id == "admin-0-boundary-bg") { |
| 51 | + if (index == possibleWorldViews.size) { |
| 52 | + index = 0 |
| 53 | + } |
| 54 | + |
| 55 | + val layerToBeAdjusted = style.getLayerAs<LineLayer>(it.id) |
| 56 | + |
| 57 | + // Use the Maps SDK's expressions to show the corresponding world view |
| 58 | + layerToBeAdjusted?.setFilter( |
| 59 | + match( |
| 60 | + get("worldview"), |
| 61 | + all(literal(possibleWorldViews[index])), |
| 62 | + literal(true), literal(false)) |
| 63 | + ) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + when (possibleWorldViews[index]) { |
| 68 | + "US" -> { |
| 69 | + Toast.makeText(this, String.format( |
| 70 | + getString(R.string.now_viewing_worldview, "US")), |
| 71 | + Toast.LENGTH_SHORT).show() |
| 72 | + } |
| 73 | + |
| 74 | + "CN" -> { |
| 75 | + Toast.makeText(this, String.format( |
| 76 | + getString(R.string.now_viewing_worldview, "China")), |
| 77 | + Toast.LENGTH_SHORT).show() |
| 78 | + } |
| 79 | + |
| 80 | + "IN" -> { |
| 81 | + Toast.makeText(this, String.format( |
| 82 | + getString(R.string.now_viewing_worldview, "India")), |
| 83 | + Toast.LENGTH_SHORT).show() |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + index++ |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public override fun onResume() { |
| 94 | + super.onResume() |
| 95 | + mapView.onResume() |
| 96 | + } |
| 97 | + |
| 98 | + override fun onStart() { |
| 99 | + super.onStart() |
| 100 | + mapView.onStart() |
| 101 | + } |
| 102 | + |
| 103 | + override fun onStop() { |
| 104 | + super.onStop() |
| 105 | + mapView.onStop() |
| 106 | + } |
| 107 | + |
| 108 | + public override fun onPause() { |
| 109 | + super.onPause() |
| 110 | + mapView.onPause() |
| 111 | + } |
| 112 | + |
| 113 | + override fun onLowMemory() { |
| 114 | + super.onLowMemory() |
| 115 | + mapView.onLowMemory() |
| 116 | + } |
| 117 | + |
| 118 | + override fun onDestroy() { |
| 119 | + super.onDestroy() |
| 120 | + mapView.onDestroy() |
| 121 | + } |
| 122 | + |
| 123 | + override fun onSaveInstanceState(outState: Bundle) { |
| 124 | + super.onSaveInstanceState(outState) |
| 125 | + mapView.onSaveInstanceState(outState) |
| 126 | + } |
| 127 | +} |
0 commit comments