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.
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");
}
}