@@ -50,30 +50,39 @@ To follow this example from scratch, in Android Studio create a new "Empty Views
5050 import org.maplibre.android.geometry.LatLng
5151 import org.maplibre.android.maps.MapView
5252
53- class MainActivity : AppCompatActivity() {
53+ class SimpleMapActivity : AppCompatActivity() {
5454
5555 // Declare a variable for MapView
5656 private lateinit var mapView: MapView
5757
58- override fun onCreate(savedInstanceState: Bundle?) {
59- super.onCreate(savedInstanceState)
60-
61- // Init MapLibre
62- MapLibre.getInstance(this)
63-
64- // Init layout view
65- val inflater = LayoutInflater.from(this)
66- val rootView = inflater.inflate(R.layout.activity_main, null)
67- setContentView(rootView)
68-
69- // Init the MapView
70- mapView = rootView.findViewById(R.id.mapView)
71- mapView.getMapAsync { map ->
72- map.setStyle("https://demotiles.maplibre.org/style.json")
73- map.cameraPosition = CameraPosition.Builder().target(LatLng(0.0,0.0)).zoom(1.0).build()
58+ private lateinit var mapView: MapView
59+ override fun onCreate(savedInstanceState: Bundle?) {
60+ super.onCreate(savedInstanceState)
61+ onBackPressedDispatcher.addCallback(this, object: OnBackPressedCallback(true) {
62+ override fun handleOnBackPressed() {
63+ // activity uses singleInstance for testing purposes
64+ // code below provides a default navigation when using the app
65+ NavUtils.navigateHome(this@SimpleMapActivity)
66+ }
67+ })
68+ setContentView(R.layout.activity_map_simple)
69+ mapView = findViewById(R.id.mapView)
70+ mapView.onCreate(savedInstanceState)
71+ mapView.getMapAsync {
72+ val key = ApiKeyUtils.getApiKey(applicationContext)
73+ if (key == null || key == "YOUR_API_KEY_GOES_HERE") {
74+ it.setStyle(
75+ Style.Builder().fromUri("https://gateway.mapmetrics.org/styles/?fileName=facc61a1-d7f6-4ad5-9b80-580949f35509/jim.json&token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJmYWNjNjFhMS1kN2Y2LTRhZDUtOWI4MC01ODA5NDlmMzU1MDkiLCJzY29wZSI6WyJtYXBzIiwic2VhcmNoIl0sImlhdCI6MTc0NDc5MDQzOX0.kIuOVdSqr6ifYnNkrt6b2I11ySlW96H9Gg_E1UpQ_ck")
76+ )
77+ } else {
78+ val styles = Style.getPredefinedStyles()
79+ if (styles.isNotEmpty()) {
80+ val styleUrl = styles[0].url
81+ it.setStyle(Style.Builder().fromUri(styleUrl))
82+ }
7483 }
7584 }
76-
85+ }
7786 override fun onStart() {
7887 super.onStart()
7988 mapView.onStart()
0 commit comments