Map Tips

Download Samples Repository

Description

This sample shows how to display a map tip for a feature layer. In this example, the MouseMove event of the MapView is handled with code that performs a FeatureLayer HitTest / Query combination which returns a single feature for display in the mapTip element defined in the XAML.

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

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.FeatureLayerMapTips"
			 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" 
					  MouseMove="MyMapView_MouseMove">
			<esri:Map InitialViewpoint="-88.35,41.61,-88.05,41.84,4326">
				<esri:ArcGISTiledMapServiceLayer
					ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />

				<esri:FeatureLayer x:Name="shelters" 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/NapervilleShelters/FeatureServer/0"
							OutFields="*" />
					</esri:FeatureLayer.FeatureTable>
				</esri:FeatureLayer>
			</esri:Map>

			<esri:MapView.Overlays>
				<esri:OverlayItemsControl>
					<Border x:Name="mapTip" Background="White" BorderBrush="Black" BorderThickness="2" 
							Padding="4" Margin="4" HorizontalAlignment="Left" VerticalAlignment="Top" 
							Visibility="Collapsed" esri:ViewBase.ViewOverlayAnchor="{Binding Geometry}">
						<StackPanel>
							<TextBlock Text="{Binding Attributes[facname]}" FontWeight="Bold" />
							<TextBlock Text="{Binding Attributes[capacity], StringFormat='Capacity: {0}'}" />
						</StackPanel>
					</Border>
				</esri:OverlayItemsControl>
			</esri:MapView.Overlays>
		</esri:MapView>
	</Grid>
</UserControl>
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace ArcGISRuntime.Samples.Desktop
{
	/// <summary>
	/// This sample shows how to display a map tip for a feature layer.  In this example, the MouseMove event of the MapView is handled with code that performs a FeatureLayer HitTest / Query combination which returns a single feature for display in the mapTip element defined in the XAML.
	/// </summary>
	/// <title>Map Tips</title>
	/// <category>Layers</category>
	/// <subcategory>Feature Layers</subcategory>
	public partial class FeatureLayerMapTips : UserControl
	{
		private bool _isMapReady;

		/// <summary>Construct Map Tips sample</summary>
		public FeatureLayerMapTips()
		{
			InitializeComponent();

			MyMapView.SpatialReferenceChanged += MyMapView_SpatialReferenceChanged;
		}

		private async void MyMapView_SpatialReferenceChanged(object sender, System.EventArgs e)
		{
			await MyMapView.LayersLoadedAsync();
			_isMapReady = true;
		}

		private async void MyMapView_MouseMove(object sender, MouseEventArgs e)
		{
			if (!_isMapReady)
				return;

			try
			{
				_isMapReady = false;

				Point screenPoint = e.GetPosition(MyMapView);
				var rows = await shelters.HitTestAsync(MyMapView, screenPoint);
				if (rows != null && rows.Length > 0)
				{
					var features = await shelters.FeatureTable.QueryAsync(rows);
					mapTip.DataContext = features.FirstOrDefault();
					mapTip.Visibility = System.Windows.Visibility.Visible;
				}
				else
					mapTip.Visibility = System.Windows.Visibility.Collapsed;
			}
			catch
			{
				mapTip.Visibility = System.Windows.Visibility.Collapsed;
			}
			finally
			{
				_isMapReady = true;
			}
		}
	}
}
Feedback on this topic?