【问题标题】:MapBox set style callback does not work sometimes and mapbox map become greyMapBox 设置样式回调有时不起作用,mapbox 地图变灰
【发布时间】:2019-10-17 14:40:40
【问题描述】:

我已经在 mapbox api 上工作了几天。我一直在 OnCreate() 方法中设置地图框。大部分时间地图已加载并且 ma​​p.setStyle 有效,但有时样式未加载并且地图变为灰色。我已经阅读了 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 不起作用以及为什么地图变灰?任何回应将不胜感激

【问题讨论】:

  • 您是否正确调用了所有生命周期方法,例如 onResumeonStart? logcat中是否有任何带有Mbgl标签的可疑日志?
  • 我也有同样的问题。你找到问题了吗?我正在调用 MapView 的生命周期方法。当我从一个地图实例返回到另一个地图实例时,似乎(很少)发生。
  • @Heinzlmaen 我认为 Lukasz Paczos 的评论解决了我的问题。我没有调用所有的生命周期方法。例如,我正在调用 mapView.onCreate(savedInstanceState) 但没有调用 mapbox 的 onStart、OnResume 等方法
  • 最后对我来说也是一样。 Mapbox 不直接在我的活动中,它仅在一些异步服务器调用后启动。因此,当活动调用 onStart 时,MapBox 尚未加载,并且调用在 if(MapBox != null) 中消失了。所以现在我在异步调用后手动调用一次 onStart 和 onResume。
  • @Heinzlmaen 你能在这里发布你的工作代码吗!

标签: android kotlin mapbox


【解决方案1】:

您是否设置了所需的访问令牌?如果不是,那可能是个问题。也许需要访问令牌来检索地图框。

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

【讨论】:

  • 我已经做到了。但对我来说是个问题。有什么解决方法吗?
【解决方案2】:

在我的例子中,onStart() 和 onResume() 在 mapView 加载之前被调用,所以我添加了一个 nullcheck:

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

显然,这意味着 mapView.onStart() 和 onResume() 最初不会被调用,因为在触发 onStart() 时不会加载 MapView。所以我手动调用一次,效果很好:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多