Kenny Sexton
Kenny Sexton

Reputation: 383

Removing a layer added to mapbox style

        mapStyle?.addSource(
            GeoJsonSource.Builder(SELECTED_ROUTE_MAP_LAYER)
                .featureCollection(pushingFeatureCollection).build()
        )


        mapStyle?.addLayer(createLineLayer(SELECTED_ROUTE_MAP_LAYER))

        mapStyle?.removeLayer(SELECTED_ROUTE_MAP_LAYER)

How do I go about removing a layer I added to my mapstyle? I'm using mapbox v10 and removeLayer does not appear to be an option anymore (Unresolved reference: removeLayer). I am trying to figure out what the recommened approach is, since the functionallity for adding a layer is the same as previous versions, yet the removal feature appears to be scraped.

Here are the versions I am using:

    // MapBox
    implementation 'com.mapbox.maps:android:10.15.0'
    implementation "com.mapbox.search:mapbox-search-android-ui:1.0.0-rc.6"

Upvotes: 0

Views: 71

Answers (1)

Kenny Sexton
Kenny Sexton

Reputation: 383

I never found a way to remove a layer once it was added, so instead I am using this workaround:

  1. Create the layer only once
        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. clear out the layer when I do not want anything displayed.
fun hideCurrentRouteLayer(mapboxMap: MapboxMap) {
    val emptyFeatureCollection = FeatureCollection.fromFeatures(emptyList())
    val source = mapboxMap.getStyle()?.getSourceAs<GeoJsonSource>(SELECTED_ROUTE_MAP_LAYER)
    source?.featureCollection(emptyFeatureCollection)
}

Upvotes: 0

Related Questions