Mapbox clustering on Android

Viewed 786

I wanted to add clusters to the mapbox map. I followed available documentation and after some time I stacked with that:

List<Feature> markerCoordinates = new ArrayList<>();
            Log.v(LOG_TAG, "all advertiesrs retrieved");

            for(Elem e: result) {
                markerCoordinates.add(Feature.fromGeometry(
                        Point.fromCoordinates(Position.fromCoordinates(e.getLatitude(), e.getLongitude())))
                );
            }

            FeatureCollection featureCollection = FeatureCollection.fromFeatures(markerCoordinates);

            Log.v(LOG_TAG, "list has: " + markerCoordinates.size() + " elements");

            FeatureCollection collection1 = FeatureCollection.fromFeatures(markerCoordinates);

            map.addSource(
                    new GeoJsonSource(CUSTOM_DATA_SOURCE,
                            collection1,
                            new GeoJsonOptions()
                                    .withCluster(true)
                                    .withClusterMaxZoom(14)
                                    .withClusterRadius(50)
                    )
            );

            // Each point range gets a different fill color.
            int[][] layers = new int[][] {
                    new int[] {10, ContextCompat.getColor(MainActivity.this, R.color.marker_red)},
                    new int[] {4, ContextCompat.getColor(MainActivity.this, R.color.marker_green)},
                    new int[] {0, ContextCompat.getColor(MainActivity.this, R.color.marker_blue)}
            };

            //Creating a marker layer for single data points
            //How to create different markers for different points categories
            Bitmap icon = BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.marker_1);
            map.addImage("marker_1", icon);

            SymbolLayer unclustered = new SymbolLayer("unclustered-points", CUSTOM_DATA_SOURCE);
            unclustered.setProperties(iconImage("marker_1"));
            map.addLayer(unclustered);

            for (int i = 0; i < layers.length; i++) {
                //Add clusters' circles
                CircleLayer circles = new CircleLayer("cluster-" + i, CUSTOM_DATA_SOURCE);
                circles.setProperties(
                        circleColor(layers[i][1]),
                        circleRadius(18f)
                );
                circles.setFilter(
                        i == 0
                                ? gte("point_count", layers[i][0]) :
                                all(gte("point_count", layers[i][0]), lt("point_count", layers[i - 1][0]))
                );
                map.addLayer(circles);
            }

            //Add the count labels
            SymbolLayer count = new SymbolLayer("count", CUSTOM_DATA_SOURCE);
            count.setProperties(
                    textField("{point_count}"),
                    textSize(12f),
                    textColor(Color.WHITE)
            );
            map.addLayer(count);

Problem is that, that I need add different icon markers for different unclustered points. For now I am able to only add one common icon marker_1. I cannot figure out how to create some "groups" of unclustered data (different icon for each different point category).

1 Answers

Try this Mapbox plugin. This lib has ClusterRenderer.java interface, you need to implement it and draw your icons.

Related