首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MapboxMap中未显示CircleLayer群集

MapboxMap中未显示CircleLayer群集
EN

Stack Overflow用户
提问于 2021-03-31 19:07:34
回答 1查看 51关注 0票数 0

我正在尝试在本教程的帮助下为MapboxMap实现CircleLayer集群:https://docs.mapbox.com/android/maps/examples/circle-layer-clustering/

在我的应用程序中,我通过API接收4种类型的路由,我已经能够在源层中显示相应的图像。现在我想为这4种类型的路线添加圆圈图层聚类,但我的圆圈图层没有在地图上渲染。太远而无法进行集群的路由按其应有的方式显示,但我的CircleLayers都没有显示出来。这是我用来绘制路由标记和聚类的代码:

代码语言:javascript
运行
复制
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没有显示出来吗?提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2021-04-01 19:14:06

问题是,我使用的是一个offline-style.json文件,该文件到目前为止还没有声明字体。所以直到我们在offline-style.json中声明一个字体,我才能在我的集群上显示这些数字。只需要注释掉符号层,该符号层显示集群上的数字,直到我们在style.json中声明一个字体。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66886626

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档