Search code examples
javaandroidmapboxmapbox-android

Why do dynamically added symbols show up grey in Mapbox


When I tap the screen, a marker gets added to the middle of a Mapbox map. The first marker shows up as it is supposed to. However as soon as I add a second marker, the markers show up grey.

First markerSecond marker turns them grey

Here is the code I use to create the symbol source and the symbol layer, as well as the onMapClickListener where the features of the symbol source get updated with the new marker. All this code is in the onStyleLoaded method.

            //Create the symbol source
            Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_map_marker_blue, null);
            Bitmap marker = BitmapUtils.getBitmapFromDrawable(drawable);
            style.addImage(BLUE_MARKER_IMAGE, marker);
            GeoJsonSource geoJsonSourceSymbol = new GeoJsonSource(SYMBOL_SOURCE_ID);
            style.addSource(geoJsonSourceSymbol);

            //Create the symbol layer
            symbolLayer = new SymbolLayer(SYMBOL_LAYER_ID, SYMBOL_SOURCE_ID);
            symbolLayer.setProperties(iconImage(BLUE_MARKER_IMAGE), iconOffset(new Float[] {0f, -10f}));
            style.addLayer(symbolLayer);

            mapboxMap.addOnMapClickListener(new MapboxMap.OnMapClickListener() {
                @Override
                public boolean onMapClick(@NonNull LatLng point) {

                    final LatLng mapTargetLatLng = mapboxMap.getCameraPosition().target;
                    Point mapTarget = Point.fromLngLat(mapTargetLatLng.getLongitude(),mapTargetLatLng.getLatitude());
                    pointList.add(mapTarget);
                    featureList.add(Feature.fromGeometry(mapTarget));

                    if (style.getLayer(SYMBOL_LAYER_ID) != null) {
                        GeoJsonSource geoJsonSourceSymbol = style.getSourceAs(SYMBOL_SOURCE_ID);
                        if (geoJsonSourceSymbol != null) {
                            geoJsonSourceSymbol.setGeoJson(FeatureCollection.fromFeatures(featureList));
                        }
                    }

                    return true;
                }
            });

Is there something I'm doing wrong or is it not possible to dynamically add symbols using data-driven styling?


Solution

  • Are you using an emulator? The Mapbox team already knows of SymbolLayer rendering issues on emulated devices:

    https://github.com/mapbox/mapbox-gl-native/issues/10829

    https://github.com/mapbox/mapbox-plugins-android/issues/1082