1

我正在尝试在我的 Android 应用程序中使用 Mapbox 在地图上显示自定义矢量图层。使用最新的 mapbox 版本。

当我通过以下方式包含图层时:

//DOES NOT WORK
binding.mapView.getMapAsync(mapboxMap -> {
        map = mapboxMap;
        VectorSource source = new VectorSource("source-id", new TileSet("2.1.0", baseUrl + "/{z}/{x}/{y}.mvt"));
        mapboxMap.addSource(source);

        LineLayer layer = new LineLayer("zones-outline", "source-id");
        layer.setSourceLayer("zones");
        layer.setProperties(
            PropertyFactory.lineWidth(2f),
            PropertyFactory.lineColor(getResources().getColor(R.color.md_blue_500))
        );
        mapboxMap.addLayer(layer);
   })

它什么也没有显示(没有登录 android 也没有登录我的服务器,就像 mapbox 甚至不知道该图层)。

但是,如果我将 addsource 和 addlayer 代码放在一个可运行的文件中,假设有 100 毫秒的延迟,它确实可以正确显示我的图层。显然,这看起来与某种并发或“初始化顺序”有关,并且延迟有效,但这不是一个好的和适当的解决方案(我想在旧设备上加载地图可能需要 100 多毫秒,也许它不会工作)。

//WORKS
binding.mapView.getMapAsync(mapboxMap -> {

    final Handler handler = new Handler();
    handler.postDelayed(() -> {
        map = mapboxMap;
        VectorSource source = new VectorSource("source-id", new TileSet("2.1.0", baseUrl + "/{z}/{x}/{y}.mvt"));
        mapboxMap.addSource(source);

        LineLayer layer = new LineLayer("zones-outline", "source-id");
        layer.setSourceLayer("zones");
        layer.setProperties(
            PropertyFactory.lineWidth(2f),
            PropertyFactory.lineColor(getResources().getColor(R.color.md_blue_500))
        );
        mapboxMap.addLayer(layer);
   }, 100)
})

是否有另一种方法/回调我应该把这个初始化?如何确定我的图层会被绘制?

4

1 回答 1

3

问题来自于我也在 onMapReady 回调中设置了一个新样式的 url(根据显示的内容动态加载)。我mapView.setStyleUrl(mapboxStyle)在地图初始化之前移动:

mapView.setStyleUrl(mapboxStyle);
binding.contestMapView.onCreate(savedInstanceState);
binding.contestMapView.getMapAsync(mapboxMap -> {
    // vector source and layer initialization
});
于 2018-06-16T20:57:46.537 回答