我正在尝试在本教程的帮助下为MapboxMap实现CircleLayer集群:https://docs.mapbox.com/android/maps/examples/circle-layer-clustering/
在我的应用程序中,我通过API接收4种类型的路由,我已经能够在源层中显示相应的图像。现在我想为这4种类型的路线添加圆圈图层聚类,但我的圆圈图层没有在地图上渲染。太远而无法进行集群的路由按其应有的方式显示,但我的CircleLayers都没有显示出来。这是我用来绘制路由标记和聚类的代码:
public void setRoutesToMap(FeatureCollection routesCollection) {
mapboxMap.getStyle(style -> {
style.removeLayer(ROUTE_MARKER_LAYER);
style.removeLayer(ROUTE_MARKER_CYCLING_CLUSTER_LAYER);
style.removeLayer(ROUTE_MARKER_MTB_CLUSTER_LAYER);
style.removeLayer(ROUTE_MARKER_SINGLETRAIL_CLUSTER_LAYER);
style.removeLayer(ROUTE_MARKER_ROADBIKE_CLUSTER_LAYER);
style.removeLayer(ROUTE_CLUSTER_COUNT_LAYER);
style.removeSource(ROUTE_MARKER_SOURCE);
// Disable any type of fading transition when icons collide on the map. This enhances the visual
// look of the data clustering together and breaking apart.
style.setTransition(new TransitionOptions(0, 0, false));
//adding source (contains feature collection from which the markers get drawn)
style.addSource(new GeoJsonSource(ROUTE_MARKER_SOURCE, routesCollection,
new GeoJsonOptions().withCluster(true)
.withClusterMaxZoom(ROUTE_CLUSTER_MAX_ZOOM)
.withClusterRadius(ROUTE_CLUSTER_RADIUS)
//adding cluster property to distinguish types of clusters depending on the features route category
.withClusterProperty(MapboxHelper.ROUTE_PROPERTY_CATEGORY_CLUSTER, Expression.sum(Expression.accumulated(), Expression.get(MapboxHelper.ROUTE_PROPERTY_CATEGORY_CLUSTER)),
Expression.switchCase(
//if route category equals BIKE_ROUTE, set cluster property to 1
Expression.eq(Expression.get(MapboxHelper.ROUTE_PROPERTY_CATEGORY), RouteProperty.Category.BIKE_ROUTE), Expression.literal(1),
//if route category equals MOUNTAINBIKE_ROUTE, set cluster property to 2
Expression.eq(Expression.get(MapboxHelper.ROUTE_PROPERTY_CATEGORY), RouteProperty.Category.MOUNTAINBIKE_ROUTE), Expression.literal(2),
//if route category equals SINGLETRAIL_ROUTE, set cluster property to 3
Expression.eq(Expression.get(MapboxHelper.ROUTE_PROPERTY_CATEGORY), RouteProperty.Category.SINGLETRAIL_ROUTE), Expression.literal(3),
//if route category equals ROADBIKE_ROUTE, set cluster property to 4
Expression.eq(Expression.get(MapboxHelper.ROUTE_PROPERTY_CATEGORY), RouteProperty.Category.ROADBIKE_ROUTE), Expression.literal(4),
//else set cluster property to 0
Expression.literal(0)))
));
//adding images with names of routes category so they get displayed with the correct category image
style.addImage(RouteProperty.Category.BIKE_ROUTE, BitmapFactory.decodeResource(getResources(), R.drawable.map_route_radwandern));
style.addImage(RouteProperty.Category.MOUNTAINBIKE_ROUTE, BitmapFactory.decodeResource(getResources(), R.drawable.map_route_mountainbike));
style.addImage(RouteProperty.Category.SINGLETRAIL_ROUTE, BitmapFactory.decodeResource(getResources(), R.drawable.map_route_singletrail));
style.addImage(RouteProperty.Category.ROADBIKE_ROUTE, BitmapFactory.decodeResource(getResources(), R.drawable.map_route_rennrad));
//adding layer for unselected markers, images are used from category feature property
style.addLayerBelow(new SymbolLayer(ROUTE_MARKER_LAYER, ROUTE_MARKER_SOURCE).withProperties(
iconImage("{" + MapboxHelper.ROUTE_PROPERTY_CATEGORY + "}"),
iconAllowOverlap(true)
), LOCATION_COMPONENT_BACKGROUND_LAYER);
//Expression to filter whether we're displaying a cluster, since only clusters contain the property "point_count"
Expression isCluster = Expression.has(MapboxHelper.PROPERTY_POINT_COUNT);
//Expression to filter for clusters with cycling type
Expression clusterCategoryCyclingFilter = Expression.eq(Expression.toNumber(Expression.get(MapboxHelper.ROUTE_PROPERTY_CATEGORY_CLUSTER)), Expression.literal(1));
//Expression to filter for clusters with mountainbike type
Expression clusterCategoryMountainbikeFilter = Expression.eq(Expression.toNumber(Expression.get(MapboxHelper.ROUTE_PROPERTY_CATEGORY_CLUSTER)), Expression.literal(2));
//Expression to filter for clusters with singletrail type
Expression clusterCategorySingletrailFilter = Expression.eq(Expression.toNumber(Expression.get(MapboxHelper.ROUTE_PROPERTY_CATEGORY_CLUSTER)), Expression.literal(3));
//Expression to filter for clusters with roadbike type
Expression clusterCategoryRoadbikeFilter = Expression.eq(Expression.toNumber(Expression.get(MapboxHelper.ROUTE_PROPERTY_CATEGORY_CLUSTER)), Expression.literal(4));
//adding circle layer for cycling cluster with proper filter
style.addLayer(new CircleLayer(ROUTE_MARKER_CYCLING_CLUSTER_LAYER, ROUTE_MARKER_SOURCE).withProperties(
circleColor(getResources().getColor(R.color.color_cycling)),
circleRadius(18f)
).withFilter(Expression.all(isCluster, clusterCategoryCyclingFilter)));
//adding circle layer for mountainbike cluster with proper filter
style.addLayer(new CircleLayer(ROUTE_MARKER_MTB_CLUSTER_LAYER, ROUTE_MARKER_SOURCE).withProperties(
circleColor(getResources().getColor(R.color.color_mountainbike)),
circleRadius(18f)
).withFilter(Expression.all(isCluster, clusterCategoryMountainbikeFilter)));
//adding circle layer for singletrail cluster with proper filter
style.addLayer(new CircleLayer(ROUTE_MARKER_SINGLETRAIL_CLUSTER_LAYER, ROUTE_MARKER_SOURCE).withProperties(
circleColor(getResources().getColor(R.color.color_singletrail)),
circleRadius(18f)
).withFilter(Expression.all(isCluster, clusterCategorySingletrailFilter)));
//adding circle layer for roadbike cluster with proper filter
style.addLayer(new CircleLayer(ROUTE_MARKER_ROADBIKE_CLUSTER_LAYER, ROUTE_MARKER_SOURCE).withProperties(
circleColor(getResources().getColor(R.color.color_roadbike)),
circleRadius(18f)
).withFilter(Expression.all(isCluster, clusterCategoryRoadbikeFilter)));
//adding cluster count layer to display the amount of routes the cluster contains
style.addLayer(new SymbolLayer(ROUTE_CLUSTER_COUNT_LAYER, ROUTE_MARKER_SOURCE).withProperties(
textField(Expression.toString(Expression.get(MapboxHelper.PROPERTY_POINT_COUNT))),
textSize(getResources().getDimension(R.dimen.text_14)),
textColor(getResources().getColor(R.color.applicationBackground)),
textIgnorePlacement(true),
textAllowOverlap(true)
));
});
if (getContext() == null) {
return;
}
presenter.generateInfoWindows(getContext(), routesCollection);
}
出于测试目的,我将maxClusterZoom设置为10。当我放大到大于10的缩放级别时,我的路线标记显示。当我再次缩小时,它们应该会聚集在一起,因为我的ClusterLayer没有显示,所以它们就消失了。你知道我做错了什么吗,或者我遗漏了什么,我的CircleLayers没有显示出来吗?提前感谢!
发布于 2021-04-01 19:14:06
问题是,我使用的是一个offline-style.json文件,该文件到目前为止还没有声明字体。所以直到我们在offline-style.json中声明一个字体,我才能在我的集群上显示这些数字。只需要注释掉符号层,该符号层显示集群上的数字,直到我们在style.json中声明一个字体。
https://stackoverflow.com/questions/66886626
复制相似问题