Petr Grenar
Petr Grenar

Reputation: 5

How to add MapBox map into the fragment in bottomnavigation | Kotlin

I am trying to add a map to my Android App. I have created basics bottom navigation the same as in this tutorial: https://www.youtube.com/watch?v=Chso6xrJ6aU. I have only added two fragments.

Now I want to add MappBox map into one of them. I am following the Mapbox quick start guide here: https://docs.mapbox.com/android/maps/overview/?q=fragment&size=n_10_n

Everything works fine, but problem is that the Map is not working. When I add the map to the main activity XML, the map loads and works. But when I add the map to one of the fragments, the map does not load. I only see the guides when I hold ctrl in the emulator.

This is the fragment with map in in:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondFragment">

<com.mapbox.mapboxsdk.maps.MapView
    android:id="@+id/map_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</com.mapbox.mapboxsdk.maps.MapView>
</androidx.constraintlayout.widget.ConstraintLayout>

This is part of MainActivity:

Mapbox.getInstance(this, getString(R.string.mapbox_access_token))

    setContentView(R.layout.activity_main)

    val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomNavigationView)
    val navController = findNavController(R.id.fragment)
    val appBarConfiguration = AppBarConfiguration(setOf(R.id.firstFragment, R.id.secondFragment))
    setupActionBarWithNavController(navController, appBarConfiguration)

    bottomNavigationView.setupWithNavController(navController)

    mapView = findViewById(R.id.map_view)
    mapView?.onCreate(savedInstanceState)
    mapView?.getMapAsync { mapboxMap ->

        mapboxMap.setStyle(Style.MAPBOX_STREETS) {

            // Map is set up and the style has loaded. Now you can add data or make other map adjustments

        }

    }

Can you help me to solve this problem?

Upvotes: 0

Views: 728

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 200120

Only the fragment that owns the MapView should be interacting with the MapView - your activity should not be involved.

Upvotes: 0

Related Questions