2
votes

I have been working from few days on mapbox api. I have been setting the mapbox in the OnCreate() method. Most of the time map is loaded and map.setStyle works but sometime style is not loaded and map becomes grey. I have read the documentation of mapbox api. It says that if mapboxMap.setStyle fails then addOnDidFailLoadingMapListener() will be called.

Following is my code:

    mapView = findViewById(R.id.mapView)
    mapView.onCreate(savedInstanceState)

    //This is mapboxMap.setStyle failure callback
    mapView.addOnDidFailLoadingMapListener {
        Toast.makeText(this, it, Toast.LENGTH_LONG).show()
    }

    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
            style ->
    //This Does not work sometimes and map becomes grey

        }

    }

I have tested it when mapBoxmap.setStyle does not set style then addOnDidFailLoadingMapListener does not trigger. Is there any idea why mapBoxmap.setStyle doest not work and why map becomes grey?. Any response will be appreciated

2
Are you correctly calling all of the lifecycle methods like onResume and onStart? Are there any suspicious logs in the logcat tagged with Mbgl? - Ɓukasz Paczos
I have the exact same issue. Did you find the problem? I am calling the lifecycle methods of MapView. Seems to happen (rarely) when I go back from one map instance to another. - Heinzlmaen
@Heinzlmaen I think my problem solved with Lukasz Paczos comment. I was not calling all the lifcycle methods. For example, i was calling mapView.onCreate(savedInstanceState) but was not calling onStart, OnResume e.t.c methods of mapbox - Developer
It was the same for me in the end. Mapbox is not directly in my activity and it only starts after some async server calls. So when the activity calls the onStart, MapBox is not yet loaded and the call disappeared in an if(MapBox != null). So now I call onStart & onResume once manually after my async calls. - Heinzlmaen
@Heinzlmaen can you post your working code here! - Ankit Jayaprakash

2 Answers

0
votes

Did you setup the required access token? If not, that could be an issue. Maybe the access token is needed to retrieve map frames.

// Mapbox access token is configured here. This needs to be called either in your application
// object or in the same activity which contains the mapview.
Mapbox.getInstance(this, getString(R.string.access_token));
0
votes

In my case, onStart() and onResume() are called before mapView is loaded, so I added a nullcheck:

public void onResume() {
    if (this.mapView != null) {
        this.mapView.onResume();
    }

Obviously, that means mapView.onStart() and onResume() are not called initially, since MapView is not loaded when onStart() is fired. So I call them manually once, and it works fine:

    this.mapView = (MapView)this.myMapView.findViewById(id.mapView);
    this.mapView.onStart();
    this.mapView.onResume();
    this.mapView.getMapAsync(this);