-2

I have problem because I have made such map with points as on example, but in my case I'd like to make such points on map with number of people per country. geojson file should have one more attribute 'number' per one co-ordinates. Is it possible? I code in Java on Android.

Example of circle layer

Geojson example file

EDIT: Now my small points are not properly clustered. Purple(5) and dark blue(4) after zoom out should gave red point with 9 number. Now I got blue point without number(I know that i count only number of points, that's why is blue).

public class CovidMapFragment extends Fragment implements CovidMapContract.View {

    private MapView mapView;
    private MapboxMap mapboxMap;
    private MainActivity activity;
    private CovidMapPresenter presenter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (MainActivity) context;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Mapbox.getInstance(activity, getString(R.string.mapbox_access_token));
        View view = inflater.inflate(R.layout.fragment_map, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mapView = activity.findViewById(R.id.mapView);

        mapView.onCreate(savedInstanceState);
        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(@NonNull MapboxMap map) {

                mapboxMap = map;

                map.setStyle(Style.LIGHT, new Style.OnStyleLoaded() {
                    @Override
                    public void onStyleLoaded(@NonNull Style style) {


                        style.setTransition(new TransitionOptions(0, 0, false));

                        mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
                                51.919438, 19.145136), 3));

                        addClusteredGeoJsonSource(style);
                        style.addImage(
                                "cross-icon-id",
                                Objects.requireNonNull(BitmapUtils.getBitmapFromDrawable(getResources().getDrawable(R.drawable.ic_circle))),
                                true
                        );

                        Toast.makeText(activity, R.string.app_name,
                                Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });


    }
    private void addClusteredGeoJsonSource(@NonNull Style loadedMapStyle) {


        try {
            loadedMapStyle.addSource(

                    new GeoJsonSource("earthquakes",
                            new URI("asset://earthquakes.geojson"),
                            new GeoJsonOptions()
                                    .withCluster(true)
                                    .withClusterMaxZoom(14)
                                    .withClusterRadius(50)
                    )
            );
        } catch (URISyntaxException uriSyntaxException) {
            Timber.e("Check the URL %s", uriSyntaxException.getMessage());
        }


        SymbolLayer unclustered = new SymbolLayer("unclustered-points", "earthquakes");

        unclustered.setProperties(
                iconImage("cross-icon-id"),
                iconSize(
                        division(
                                get("cases"), literal(4.0f)
                        )
                ),
                iconColor(
                        interpolate(exponential(1), get("cases"),
                                stop(2.0, rgb(0, 255, 0)),
                                stop(4, rgb(0, 0, 255)),
                                stop(7.0, rgb(255, 0, 0))
                        )
                )
        );
        unclustered.setFilter(has("cases"));
        loadedMapStyle.addLayer(unclustered);


        int[][] layers = new int[][] {
                new int[] {7, ContextCompat.getColor(activity.getApplicationContext(), R.color.red)},
                new int[] {4, ContextCompat.getColor(activity.getApplicationContext(), R.color.green)},
                new int[] {0, ContextCompat.getColor(activity.getApplicationContext(), R.color.blue)}
        };

        for (int i = 0; i < layers.length; i++) {

            CircleLayer circles = new CircleLayer("cluster-" + i, "earthquakes");
            circles.setProperties(
                    circleColor(layers[i][1]),
                    circleRadius(18f)
            );

            Expression pointCount = toNumber(get("point_count"));


            circles.setFilter(
                    i == 0
                            ? all(has("point_count"),

                            gte(pointCount, literal(layers[i][0]))
                    ) : all(has("point_count"),

                            gte(pointCount, literal(layers[i][0])),
                            lt(pointCount, literal(layers[i - 1][0]))
                    )
            );
            loadedMapStyle.addLayer(circles);
        }


        SymbolLayer count = new SymbolLayer("count", "earthquakes");
        count.setProperties(
                //textField(Expression.toString(get("point_count"))),
                textField(Expression.toString(get("cases"))),
                textSize(12f),
                textColor(Color.WHITE),
                textIgnorePlacement(true),
                textAllowOverlap(true)
        );
        loadedMapStyle.addLayer(count);
    }

    @Override
    public void onStart() {
        super.onStart();
        mapView.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
        mapView.onStop();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }


    @Override
    public void showMessage(String message) {
        System.out.println("Fragment");
    }


}

Actual effect 1

Actual effect 2

geojson file

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Worldmaster
  • 215
  • 2
  • 11

1 Answers1

1

Yes, this is possible to achieve by adding a "numbers" property to each of the Feature's "properties" object. You can then modify the expressions in the linked example to use this new property for displaying labels and clustering the points.

This expressions overview is a useful guide for getting started with writing your own expressions for the Mapbox Maps SDK for Android. Expressions provide you with a way to dynamically style spatial data based on the data's properties. The complete expressions reference can be found here in the Mapbox style specification.