我有问题,因为我已经举例说明了这样的地图,但就我而言,我想在地图上列出每个国家的人数。 geojson文件应每一个坐标具有一个更多的属性“数字”。可能吗?我在Android上使用Java编写代码。
编辑:现在,我的小观点没有正确地聚类。缩小后的紫色(5)和深蓝色(4)应该给出9点的红点。现在我得到了没有数字的蓝点(我知道我只计算点数,这就是为什么是蓝色)。
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");
}
}
是,这可以通过向"numbers"
的Feature
对象中的每个对象添加"properties"
属性来实现。然后,您可以在链接的示例中修改表达式,以使用此新属性来显示标签和将点聚类。
[expressions overview是有用的指南,可帮助您开始为Android Mapbox Maps SDK编写自己的表达式。表达式为您提供了一种根据数据属性动态设置空间数据样式的方法。完整的表达式参考可在Mapbox样式规范中找到here。