android - MapBox 设置样式回调有时不起作用,mapbox map 变灰

标签 android kotlin mapbox

几天来我一直在研究 mapbox api。我一直在 OnCreate() 方法中设置 map 框。大多数情况下, map 已加载且 ma​​p.setStyle 有效,但有时未加载样式且 map 变为灰色。我已经阅读了 mapbox api 的文档。它表示如果 mapboxMap.setStyle 失败,则将调用 addOnDidFailLoadingMapListener()

以下是我的代码:

    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

        }

    }

我测试过当 ma​​pBoxmap.setStyle 没有设置样式时 addOnDidFailLoadingMapListener 不会触发。知道为什么 ma​​pBoxmap.setStyle 不起作用以及为什么 map 变成灰色吗?任何回应将不胜感激

最佳答案

您是否设置了所需的访问 token ?如果没有,那可能是个问题。可能需要访问 token 来检索 map 框。

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

关于android - MapBox 设置样式回调有时不起作用,mapbox map 变灰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58435208/

相关文章:

java - 无法在单击按钮时替换 fragment

android - 带有 ActionBar 的 NoSuchMethodError 异常

android - 没有收到有关 PagedList 更新的通知

java - 替换 Kotlin 原生解决方案中的流收集

android - 我的 android 应用程序强制关闭, toast 和操作栏搜索

android - 公共(public)应用程序上的 Facebook key 散列无效

java - Dagger 1 不会现场将 Java 类注入(inject) Kotlin Activity

data-visualization - 使用 Mapbox GL JS 切换图层

javascript - 将拖动事件从 div 传递到 mapbox 层

ios - 将标注/语音气泡添加到 MapView 的用户注释