简体   繁体   English

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

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

I have two *.geojson-files in my assets-folder and I am loading these points to the map.我的资产文件夹中有两个 *.geojson 文件,我将这些点加载到 map。 But I am trying to change the default mapbox-markers to custom colored ones (eg green and black markers).但我正在尝试将默认的地图框标记更改为自定义颜色的标记(例如绿色和黑色标记)。

I created a marker as a vector asset, but when I use that instead of "R.drawable.map_marker_light" I get the following error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap$Config android.graphics.Bitmap.getConfig()' on a null object reference我创建了一个标记作为矢量资产,但是当我使用它而不是“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

I couldn't find a current solution for my problem.我找不到我的问题的当前解决方案。

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();
                }
            }
        });
    }

Ok i found the solution through github.好的,我通过 github 找到了解决方案。 I added the following code to my MainActivity and adjusted the if-else part.我将以下代码添加到我的 MainActivity 并调整了 if-else 部分。 Then I got my custom markers.然后我得到了我的自定义标记。

    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