Select to view content in your preferred language

Selecting a feature from FeatureCollectionLayer

757
3
Jump to solution
07-24-2023 01:43 PM
DavidCasillas
Emerging Contributor

Hi,

I'm using a FeatureCollectionLayer to load custom geometry and attributes on my map. I have no problem displaying points on the map. But I do have an issue when selecting a feature.

Here is how I'm calling the tap method:

lifecycleScope.launch {
            mapView.onSingleTapConfirmed.collect { event ->
                selectFeature(event.screenCoordinate)
            }
        }

Here's the method to select feature:

private suspend fun selectFeature(coordinate: ScreenCoordinate) {
        val layer = featureCollectionLayer.layers.value[0]
        layer.clearSelection()
        val layerResult = mapView.identifyLayer(layer, coordinate, 25.0, false, -1)
        layerResult.apply {
            onSuccess { result ->
                val features = result.geoElements.filterIsInstance<Feature>()
                if (features.isEmpty()) {
                    Log.d(localClassName, "No feature selected")
                    return@onSuccess
                }
                val selectedFeature = features[0]
                layer.selectFeatures(features)
                Snackbar.make(
                    mapView,
                    "${selectedFeature.attributes["Name"]} selected",
                    Snackbar.LENGTH_SHORT
                ).show()
            }
            onFailure {
                val errorMessage = "Select feature failed: " + it.message
                Log.e(localClassName, errorMessage)
                Snackbar.make(mapView, errorMessage, Snackbar.LENGTH_SHORT).show()
            }
        }
    }

Based on what I have logged ("No feature selected"), it seems there are no features, but the points do display. If anyone can see an issue, that would be greatly appreciated.

Thanks,

Dave

1 Solution

Accepted Solutions
DavidCasillas
Emerging Contributor

I was able to figure out how to select the features. Here's an updated code:

private suspend fun selectFeature(coordinate: ScreenCoordinate) {
        val layer = featureCollectionLayer.layers.value[0]
        layer.clearSelection()
        val layerResult = mapView.identifyLayer(featureCollectionLayer, coordinate, 25.0, false, -1)
        layerResult.apply {
            onSuccess { result ->
                val features = mutableListOf<Feature>()
                    result.sublayerResults.forEach { subResults ->
                        features.addAll(subResults.geoElements.filterIsInstance<Feature>())
                    }
                if (features.isEmpty()) {
                    Log.d(localClassName, "No feature selected")
                    return@onSuccess
                }
                val selectedFeature = features[0]
                layer.selectFeatures(features)
                Snackbar.make(
                    mapView,
                    "${selectedFeature.attributes["Name"]} selected",
                    Snackbar.LENGTH_SHORT
                ).show()
            }
            onFailure {
                val errorMessage = "Select feature failed: " + it.message
                Log.e(localClassName, errorMessage)
                Snackbar.make(mapView, errorMessage, Snackbar.LENGTH_SHORT).show()
            }
        }
    }

I guess when selecting features from a FeatureCollectionLayer, you must identify those features from the sublayerResults.

View solution in original post

0 Kudos
3 Replies
PuneetPrakash
Esri Contributor

Maybe the points that are displaying are on a different FeatureLayer that you are performing the Identify operation on ?
You can try using identify layers function instead. This will perform identify operation on all layers in the view.

0 Kudos
DavidCasillas
Emerging Contributor

Thank you for the response. I was able to identify the feature collection layers on my map with no issues. I'm still not getting anything when tapping on a feature.

0 Kudos
DavidCasillas
Emerging Contributor

I was able to figure out how to select the features. Here's an updated code:

private suspend fun selectFeature(coordinate: ScreenCoordinate) {
        val layer = featureCollectionLayer.layers.value[0]
        layer.clearSelection()
        val layerResult = mapView.identifyLayer(featureCollectionLayer, coordinate, 25.0, false, -1)
        layerResult.apply {
            onSuccess { result ->
                val features = mutableListOf<Feature>()
                    result.sublayerResults.forEach { subResults ->
                        features.addAll(subResults.geoElements.filterIsInstance<Feature>())
                    }
                if (features.isEmpty()) {
                    Log.d(localClassName, "No feature selected")
                    return@onSuccess
                }
                val selectedFeature = features[0]
                layer.selectFeatures(features)
                Snackbar.make(
                    mapView,
                    "${selectedFeature.attributes["Name"]} selected",
                    Snackbar.LENGTH_SHORT
                ).show()
            }
            onFailure {
                val errorMessage = "Select feature failed: " + it.message
                Log.e(localClassName, errorMessage)
                Snackbar.make(mapView, errorMessage, Snackbar.LENGTH_SHORT).show()
            }
        }
    }

I guess when selecting features from a FeatureCollectionLayer, you must identify those features from the sublayerResults.

0 Kudos