Hit Testing

Download Samples Repository

Description

This sample demonstrates feature layer hit testing. When the user clicks on the map, the application uses FeatureLayer.HitTest to retrieve the feature at the mouse point. The HitTest method returns the row ID of the first feature found at the given point. This sample then uses the FeatureTable.QueryAsync method to retrieve a Feature for the HitTested row ID and displays its attributes in the UI.

"Desktop" "Store" "Phone" Available for Desktop, Store, Phone

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.FeatureLayerHitTesting"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013">
    <Grid>
		<esri:MapView x:Name="MyMapView" WrapAround="True" 
					  MapViewTapped="MyMapView_MapViewTapped">
			<esri:Map InitialViewpoint="-14675766.3566695,2695407.73380258,-6733121.86117095,6583994.1013904">
                <esri:ArcGISTiledMapServiceLayer
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />

                <esri:FeatureLayer ID="FeatureLayer">
					<esri:FeatureLayer.Renderer>
						<esri:SimpleRenderer>
							<esri:SimpleMarkerSymbol Color="LightBlue" Style="Circle" Size="12">
								<esri:SimpleMarkerSymbol.Outline>
									<esri:SimpleLineSymbol Color="Blue" Width="2" Style="Solid" />
								</esri:SimpleMarkerSymbol.Outline>
							</esri:SimpleMarkerSymbol>
						</esri:SimpleRenderer>
					</esri:FeatureLayer.Renderer>
					<esri:FeatureLayer.FeatureTable>
                        <esri:ServiceFeatureTable
                            ServiceUri="http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/0"
                            Where="pop2000 > 200000" OutFields="*" />
                    </esri:FeatureLayer.FeatureTable>
                </esri:FeatureLayer>
            </esri:Map>
        </esri:MapView>

		<Border Background="White" BorderBrush="Black" BorderThickness="1"
				HorizontalAlignment="Right" VerticalAlignment="Top" 
				Margin="30" Padding="20">
			<Border.Effect>
				<DropShadowEffect />
			</Border.Effect>
			<StackPanel>
                <TextBlock Text="HitTest Feature" FontSize="14" FontWeight="Bold" />
                <ListView ItemsSource="{Binding ResultFeature.Attributes}" Margin="5">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Grid.Column="0" Width="75" Text="{Binding Key}" />
                                <TextBlock Grid.Column="1" Width="175" Text="{Binding Value}" FontWeight="Bold" />
                            </StackPanel>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </StackPanel>
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Layers;
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// This sample demonstrates feature layer hit testing. When the user clicks on the map, the application uses FeatureLayer.HitTest to retrieve the feature at the mouse point. The HitTest method returns the row ID of the first feature found at the given point. This sample then uses the FeatureTable.QueryAsync method to retrieve a Feature for the HitTested row ID and displays its attributes in the UI.
    /// </summary>
    /// <title>Hit Testing</title>
	/// <category>Layers</category>
	/// <subcategory>Feature Layers</subcategory>
	public partial class FeatureLayerHitTesting : UserControl, INotifyPropertyChanged
    {
        /// <summary>PropertyChanged event for INotifyPropertyChanged</summary>
        public event PropertyChangedEventHandler PropertyChanged;

        private Feature _resultFeature;
        private FeatureLayer _featureLayer;

        /// <summary>HitTest result feature</summary>
        public Feature ResultFeature 
        {
            get { return _resultFeature; }
            set
            {
                _resultFeature = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("ResultFeature"));
            }
        }

        /// <summary>Construct FeatureLayerHitTesting sample control</summary>
        public FeatureLayerHitTesting()
        {
            InitializeComponent();

            DataContext = this;
            _featureLayer = MyMapView.Map.Layers["FeatureLayer"] as FeatureLayer;
        }

        /// <summary>
        /// On each mouse click:
        /// - HitTest the feature layer
        /// - Query the feature table for the returned row
        /// - Set the result feature for the UI
        /// </summary>
		private async void MyMapView_MapViewTapped(object sender, MapViewInputEventArgs e)
        {
            try
            {
				var rows = await _featureLayer.HitTestAsync(MyMapView, e.Position);
                if (rows != null && rows.Length > 0)
                {
                    // Forcing query to be executed against local cache
                    var features = await (_featureLayer.FeatureTable as ServiceFeatureTable).QueryAsync(rows, true);

                    ResultFeature = features.FirstOrDefault();
                }
                else
                    ResultFeature = null;
            }
            catch (Exception ex)
            {
                ResultFeature = null;
                MessageBox.Show("HitTest Error: " + ex.Message, "Feature Layer Hit Testing Sample");
            }
        }
    }
}
Feedback on this topic?