Identify features in a scene to select.
Use case
You can select features to visually distinguish them with a selection color or highlighting. This can be useful to demonstrate the physical extent or associated attributes of a feature, or to initiate another action such as centering that feature in the scene view.
How to use the sample
Tap on a building in the scene layer to select it. Deselect buildings by clicking away from the buildings.
How it works
- Create an
ArcGISSceneLayer
passing in the URL to a scene layer service. - Wait for the user to tap with the
sceneView.GeoViewTapped
event and get the tapped screen point. - Call
sceneView.IdentifyLayersAsync(sceneLayer, screenPoint, tolerance, false, 1)
to identify features in the scene. - From the resulting
IdentifyLayerResult
, get the list of identifiedGeoElements
withresult.GeoElements
. - Get the first element in the list, checking that it is a feature, and call
sceneLayer.SelectFeature(feature)
to select it.
Relevant API
- ArcGISSceneLayer
- Scene
- SceneView
About the data
This sample shows a Berlin, Germany Scene hosted on ArcGIS Online.
Tags
3D, Berlin, buildings, identify, model, query, search, select
Sample Code
// Copyright 2018 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.
using Android.App;
using Android.OS;
using Android.Widget;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.UI.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using Esri.ArcGISRuntime.Geometry;
using Android.Views;
using Surface = Esri.ArcGISRuntime.Mapping.Surface;
namespace ArcGISRuntime.Samples.SceneLayerSelection
{
[Activity (ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
[ArcGISRuntime.Samples.Shared.Attributes.Sample(
name: "Scene layer selection",
category: "Layers",
description: "Identify features in a scene to select.",
instructions: "Tap on a building in the scene layer to select it. Deselect buildings by clicking away from the buildings.",
tags: new[] { "3D", "Berlin", "buildings", "identify", "model", "query", "search", "select" })]
public class SceneLayerSelection : Activity
{
// Hold a reference to the scene view.
private SceneView _mySceneView;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Title = "Scene layer selection";
CreateLayout();
Initialize();
}
private async void Initialize()
{
// Create a new Scene with an imagery basemap.
Scene scene = new Scene(BasemapStyle.ArcGISImageryStandard);
// Add a base surface with elevation data.
Surface elevationSurface = new Surface();
Uri elevationService = new Uri("https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer");
elevationSurface.ElevationSources.Add(new ArcGISTiledElevationSource(elevationService));
scene.BaseSurface = elevationSurface;
// Add a scene layer.
Uri buildingsService = new Uri("https://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/Buildings_Berlin/SceneServer");
ArcGISSceneLayer buildingsLayer = new ArcGISSceneLayer(buildingsService);
scene.OperationalLayers.Add(buildingsLayer);
// Assign the Scene to the SceneView.
_mySceneView.Scene = scene;
try
{
// Create a camera with an interesting view.
await buildingsLayer.LoadAsync();
MapPoint center = (MapPoint)GeometryEngine.Project(buildingsLayer.FullExtent.GetCenter(), SpatialReferences.Wgs84);
Camera viewCamera = new Camera(center.Y, center.X, 600, 120, 60, 0);
// Set the viewpoint with the camera.
_mySceneView.SetViewpointCamera(viewCamera);
// Listen for taps.
_mySceneView.GeoViewTapped += SceneViewTapped;
}
catch (Exception e)
{
new AlertDialog.Builder(this).SetMessage(e.ToString()).SetTitle("Error").Show();
}
}
private async void SceneViewTapped(object sender, GeoViewInputEventArgs e)
{
// Get the scene layer from the scene (first and only operational layer).
ArcGISSceneLayer sceneLayer = (ArcGISSceneLayer)_mySceneView.Scene.OperationalLayers.First();
// Clear any existing selection.
sceneLayer.ClearSelection();
try
{
// Identify the layer at the tap point.
// Use a 10-pixel tolerance around the point and return a maximum of one feature.
IdentifyLayerResult result = await _mySceneView.IdentifyLayerAsync(sceneLayer, e.Position, 10, false, 1);
// Get the GeoElements that were identified (will be 0 or 1 element).
IReadOnlyList<GeoElement> geoElements = result.GeoElements;
// If a GeoElement was identified, select it in the scene.
if (geoElements.Any())
{
GeoElement geoElement = geoElements.FirstOrDefault();
if (geoElement != null)
{
// Select the feature to highlight it in the scene view.
sceneLayer.SelectFeature((Feature)geoElement);
}
}
}
catch (Exception ex)
{
new AlertDialog.Builder(this).SetMessage(ex.ToString()).SetTitle("Error").Show();
}
}
private void CreateLayout()
{
// Create a new vertical layout for the app.
var layout = new LinearLayout(this) { Orientation = Orientation.Vertical };
// Create and add a help label.
TextView helpLabel = new TextView(this)
{
Text = "Tap to select buildings."
};
layout.AddView(helpLabel);
// Add the scene view to the layout.
_mySceneView = new SceneView(this);
layout.AddView(_mySceneView);
// Show the layout in the app.
SetContentView(layout);
}
protected override void OnDestroy()
{
base.OnDestroy();
// Remove the sceneview
(_mySceneView.Parent as ViewGroup).RemoveView(_mySceneView);
_mySceneView.Dispose();
_mySceneView = null;
}
}
}