簡體   English   中英

將標記圖標更改為自定義圖標 | Mapbox SDK

[英]Change the Marker Icon to a custom Icon | Mapbox SDK

我的資產文件夾中有兩個 *.geojson 文件,我將這些點加載到 map。 但我正在嘗試將默認的地圖框標記更改為自定義顏色的標記(例如綠色和黑色標記)。

我創建了一個標記作為矢量資產,但是當我使用它而不是“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();
                }
            }
        });
    }

好的,我通過 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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM