将标记图标更改为自定义图标 |地图箱SDK

时间:2021-01-10 16:43:47

标签: android mapbox marker mapbox-android

我的资产文件夹中有两个 *.geojson 文件,我正在将这些点加载到地图上。但我正在尝试将默认的 mapbox 标记更改为自定义颜色的标记(例如绿色和黑色标记)。

我创建了一个标记作为矢量资产,但是当我使用它而不是“R.drawable.map_marker_light”时,我收到以下错误:java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap$Config android.graphics.Bitmap.getConfig()' on a null object reference

我找不到针对我的问题的当前解决方案。

public void GeoJSONToMap(final String sourceId, final String layerId, final String asset_id) {
        mapboxMap.getStyle(new Style.OnStyleLoaded() {
            @Override
            public void onStyleLoaded(@NonNull Style style) {

                try {
                    GeoJsonSource source = new GeoJsonSource(sourceId, new URI(asset_id));

                    style.addSource(source);

                    Bitmap icon;


                    if (layerId.equals("first-layer-id")) {
                        icon = BitmapFactory.decodeResource(getResources(), R.drawable.mapbox_marker_icon_default);
                    } else {
                        icon = BitmapFactory.decodeResource(getResources(), R.drawable.map_marker_light);
                    }

                    style.addImage(layerId + " marker", icon);

                    SymbolLayer symbolLayer = new SymbolLayer(layerId, sourceId);

                    symbolLayer.setProperties(
                            iconImage(layerId + " marker"),
                            iconAllowOverlap(true),
                            iconIgnorePlacement(true)
                    );

                    style.addLayer(symbolLayer);

                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

好的,我通过 github 找到了解决方案。我将以下代码添加到我的 MainActivity 和 调整了 if-else 部分。然后我得到了我的自定义标记。

    public Bitmap getBitmap() {
        Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.marker_book, null);
        Bitmap mBitmap = BitmapUtils.getBitmapFromDrawable(drawable);

        if (mBitmap != null && mBitmap.getConfig() != Bitmap.Config.ARGB_8888) {
            mBitmap = mBitmap.copy(Bitmap.Config.ARGB_8888, false);
        }
        return mBitmap;
    }
相关问题