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
Solved! Go to Solution.
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.
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.
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.
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.