删除添加到地图框样式的图层

问题描述 投票:0回答:1
        mapStyle?.addSource(
            GeoJsonSource.Builder(SELECTED_ROUTE_MAP_LAYER)
                .featureCollection(pushingFeatureCollection).build()
        )


        mapStyle?.addLayer(createLineLayer(SELECTED_ROUTE_MAP_LAYER))

        mapStyle?.removeLayer(SELECTED_ROUTE_MAP_LAYER)

如何删除添加到地图样式中的图层?我正在使用 mapbox v10,而

removeLayer
似乎不再是一个选项 (
Unresolved reference: removeLayer
)。我试图弄清楚推荐的方法是什么,因为添加图层的功能与以前的版本相同,但删除功能似乎被取消了。

以下是我正在使用的版本:

    // MapBox
    implementation 'com.mapbox.maps:android:10.15.0'
    implementation "com.mapbox.search:mapbox-search-android-ui:1.0.0-rc.6"
mapbox mapbox-android mapbox-android-maps
1个回答
0
投票

我从未找到添加图层后将其删除的方法,因此我使用此解决方法:

  1. 仅创建图层一次
        val layerSource =
            mapView.getMapboxMap().getStyle()?.getSourceAs<GeoJsonSource>(SELECTED_ROUTE_MAP_LAYER)

        if (layerSource == null) {
            mapStyle?.addSource(
                GeoJsonSource.Builder(SELECTED_ROUTE_MAP_LAYER)
                    .featureCollection(pushingFeatureCollection).build()
            )

            // Add layer above rendered routes
            mapStyle?.let { addLayerBasedOnMapType(this, it, SELECTED_ROUTE_MAP_LAYER) }

        } else {
            layerSource.featureCollection(pushingFeatureCollection)
        }
  1. 当我不想显示任何内容时清除图层。
fun hideCurrentRouteLayer(mapboxMap: MapboxMap) {
    val emptyFeatureCollection = FeatureCollection.fromFeatures(emptyList())
    val source = mapboxMap.getStyle()?.getSourceAs<GeoJsonSource>(SELECTED_ROUTE_MAP_LAYER)
    source?.featureCollection(emptyFeatureCollection)
}
© www.soinside.com 2019 - 2024. All rights reserved.