Developer
Developer

Reputation: 265

MapBox set style callback does not work sometimes and mapbox map become grey

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

Upvotes: 2

Views: 1259

Answers (2)

Heinzlmaen
Heinzlmaen

Reputation: 967

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);

Upvotes: 0

Jarvis
Jarvis

Reputation: 402

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));

Upvotes: 0

Related Questions