在 android 上向 Mapbox 地图添加多个标记,无法将源添加到 mapboxMap

Adding multiple markers to a Mapbox Map on android, cannot add source to mapboxMap

按照本教程 SymbolLayer 我已经根据从数据库中获取的对象列表创建了一个 geoJson 字符串。当我需要打电话时

mapboxMap.addSource(source);

我找不到我的地图的 addSource 方法。也不是 addLayer 一个。我继续做的是在名为 style.addSource 和 style.addLayer 的 onMapReady 方法中,但它不起作用。这是它的样子:

public void onMapReady(@NonNull final MapboxMap mapboxMap) {
    MainActivity.this.mapboxMap = mapboxMap;
    Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_fuel_11, null);
    Bitmap bitmap = BitmapUtils.getBitmapFromDrawable(drawable); 
  //got the image to display
    String geoJsonData = toJson(getDistr()); 
  //got the geoJson String
    FeatureCollection featureCollection = FeatureCollection.fromJson(geoJsonData);
    Source source = new GeoJsonSource("id", featureCollection);
    SymbolLayer symbolLayer = new SymbolLayer("layerId", "id");

    mapboxMap.setStyle(new Style.Builder().fromUri("mapbox://styles/mapbox/navigation-preview-day-v4"),
            new Style.OnStyleLoaded() {
                @Override
                public void onStyleLoaded(@NonNull Style style) {
                    style.addImage("img", bitmap);
                    style.addSource(source);
                    style.addLayer(symbolLayer.withProperties(PropertyFactory.iconImage("img")));
                    enableLocationComponent(style);
                }
            });

任何人都可以说出我做错了什么,或者我应该采取什么其他方法来将我在数据库中的对象列表以及纬度和经度显示到地图中?

https://docs.mapbox.com/android/maps/examples/marker-symbol-layer/

尝试将以下代码移动到 onStyleLoaded() 回调而不是 onMapReady()。在 style.addImage("img", bitmap); 行之前尝试 运行。

MainActivity.this.mapboxMap = mapboxMap;
    Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_fuel_11, null);
    Bitmap bitmap = BitmapUtils.getBitmapFromDrawable(drawable); 
  //got the image to display
    String geoJsonData = toJson(getDistr()); 
  //got the geoJson String
    FeatureCollection featureCollection = FeatureCollection.fromJson(geoJsonData);
    Source source = new GeoJsonSource("id", featureCollection);
    SymbolLayer symbolLayer = new SymbolLayer("layerId", "id");