This sample demonstrates how to retrieve contents from a webmap on ArcGIS Online that contains feature service layer with pop-ups. Since feature service layers are represented as feature layers in the map, popup templates are applied to map tips configured on the layer. A set of attached properties on the Document class enable access to feature layer properties within a webmap. This includes popup template details provided as a data template via the PopupTemplate attached property. Attribute values of features in the feature layer can be used to populate the data template. In this sample, a feature layer represents a feature service layer on which pop-ups are configured. You can choose to show attribute data as map tips or in an info window using the PopupTemplate data template to define display.
Download Sample Application
< UserControl x:Class = " ArcGISWPFSDK.WebMapFeatureServicePopups "
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/client/2009 " >
< Grid x:Name = " LayoutRoot " Background = " White " >
< Grid.Resources >
< LinearGradientBrush x:Key = " PanelGradient " EndPoint = " 0.5,1 " StartPoint = " 0.5,0 " >
< LinearGradientBrush.RelativeTransform >
< TransformGroup >
< ScaleTransform CenterY = " 0.5 " CenterX = " 0.5 " />
< SkewTransform CenterY = " 0.5 " CenterX = " 0.5 " />
< RotateTransform Angle = " 176 " CenterY = " 0.5 " CenterX = " 0.5 " />
< TranslateTransform />
</ TransformGroup >
</ LinearGradientBrush.RelativeTransform >
< GradientStop Color = " #FF145787 " Offset = " 0.16 " />
< GradientStop Color = " #FF3D7FAC " Offset = " 0.502 " />
< GradientStop Color = " #FF88C5EF " Offset = " 0.984 " />
</ LinearGradientBrush >
</ Grid.Resources >
< esri : Map x:Name = " MyMap " />
< esri : InfoWindow x:Name = " MyInfoWindow "
Padding = " 2 "
CornerRadius = " 10 "
Map = " {Binding ElementName=MyMap} " >
< esri : InfoWindow.Background >
< LinearGradientBrush EndPoint = " 1.038,1.136 " StartPoint = " 0.015,0.188 " >
< GradientStop Color = " #FFD1DFF2 " />
< GradientStop Color = " #FF666666 " Offset = " 0.946 " />
</ LinearGradientBrush >
</ esri : InfoWindow.Background >
</ esri : InfoWindow >
< Grid HorizontalAlignment = " Right " VerticalAlignment = " Top " Margin = " 0,15,15,0 " >
< Rectangle Fill = " {StaticResource PanelGradient} " Stroke = " Gray " RadiusX = " 10 " RadiusY = " 10 " Margin = " 0,0,0,5 " >
< Rectangle.Effect >
< DropShadowEffect />
</ Rectangle.Effect >
</ Rectangle >
< Rectangle Fill = " #FFFFFFFF " Stroke = " DarkGray " RadiusX = " 5 " RadiusY = " 5 " Margin = " 10,10,10,15 " />
< StackPanel Orientation = " Vertical " Margin = " 25,20,25,25 " >
< TextBlock Text = " Display Attributes In: " Foreground = " Black " FontWeight = " Bold " FontSize = " 12 " Margin = " 3 " />
< RadioButton x:Name = " MapTipRB " GroupName = " MyRadioButtons " Checked = " MapTipRadioButton_Checked " IsChecked = " True " Content = " Map Tip " Margin = " 2 " />
< RadioButton GroupName = " MyRadioButtons " Checked = " InfoWindowRadioButton_Unchecked " Content = " Info Window " Margin = " 2 " />
</ StackPanel >
</ Grid >
</ Grid >
</ UserControl >
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.WebMap;
using System.Windows.Media;
namespace ArcGISWPFSDK
{
public partial class WebMapFeatureServicePopups : UserControl
{
Dictionary<string , FrameworkElement> mapTipsElements = new Dictionary<string , FrameworkElement>();
MapPoint lastPoint = null ;
public WebMapFeatureServicePopups()
{
InitializeComponent();
Document webMap = new Document();
webMap.GetMapCompleted += webMap_GetMapCompleted;
webMap.GetMapAsync("cb7f84a339c54e7bb160027de51ceb7e" );
}
void webMap_GetMapCompleted(object sender, GetMapCompletedEventArgs e)
{
if (e.Error == null )
{
MyMap.Extent = e.Map.Extent;
int i = 0;
LayerCollection layerCollection = new LayerCollection();
foreach (Layer layer in e.Map.Layers)
{
layer.ID = i.ToString();
if (layer is FeatureLayer)
{
Border maptip = (layer as FeatureLayer).MapTip as Border;
ContentControl scv = maptip.Child as ContentControl;
scv.Foreground = new SolidColorBrush(Colors.Black);
mapTipsElements.Add(layer.ID, maptip);
}
layerCollection.Add(layer);
i++;
}
e.Map.Layers.Clear();
MyMap.Layers = layerCollection;
}
}
private void MapTipRadioButton_Checked(object sender, RoutedEventArgs e)
{
if (MyMap != null )
{
MyInfoWindow.IsOpen = false ;
foreach (Layer layer in MyMap.Layers)
{
if (layer is FeatureLayer)
(layer as FeatureLayer).MouseLeftButtonUp -= WebMapFeatureServicePopups_MouseLeftButtonUp;
if (mapTipsElements.ContainsKey(layer.ID))
(layer as FeatureLayer).MapTip = mapTipsElements[layer.ID];
}
}
}
private void InfoWindowRadioButton_Unchecked(object sender, RoutedEventArgs e)
{
foreach (Layer layer in MyMap.Layers)
if (layer is FeatureLayer)
{
(layer as FeatureLayer).MapTip = null ;
(layer as FeatureLayer).MouseLeftButtonUp += WebMapFeatureServicePopups_MouseLeftButtonUp;
}
}
void WebMapFeatureServicePopups_MouseLeftButtonUp(object sender, GraphicMouseButtonEventArgs e)
{
FeatureLayer flayer = sender as FeatureLayer;
MapPoint clickPoint = MyMap.ScreenToMap(e.GetPosition(MyMap));
if (clickPoint != lastPoint)
{
if (flayer.GetValue(Document.PopupTemplateProperty) != null )
{
DataTemplate dt = flayer.GetValue(Document.PopupTemplateProperty) as DataTemplate;
MyInfoWindow.Anchor = clickPoint;
MyInfoWindow.ContentTemplate = dt;
MyInfoWindow.Foreground = new SolidColorBrush(Colors.Black);
MyInfoWindow.Content = e.Graphic.Attributes;
MyInfoWindow.IsOpen = true ;
MyInfoWindow.Background = LayoutRoot.Resources["PanelGradient" ] as Brush;
lastPoint = clickPoint;
}
}
}
}
}
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Geometry
Imports ESRI.ArcGIS.Client.WebMap
Imports System.Windows.Media
Namespace ArcGISWPFSDK
Partial Public Class WebMapFeatureServicePopups
Inherits UserControl
Private mapTipsElements As New Dictionary(Of String , FrameworkElement)()
Private lastPoint As MapPoint = Nothing
Public Sub New ()
InitializeComponent()
Dim webMap As New Document()
AddHandler webMap.GetMapCompleted, AddressOf webMap_GetMapCompleted
webMap.GetMapAsync("cb7f84a339c54e7bb160027de51ceb7e" )
End Sub
Private Sub webMap_GetMapCompleted(sender As Object , e As GetMapCompletedEventArgs)
If e.[Error ] Is Nothing Then
MyMap.Extent = e.Map.Extent
Dim i As Integer = 0
Dim layerCollection As New LayerCollection()
For Each layer As Layer In e.Map.Layers
layer.ID = i.ToString()
If TypeOf layer Is FeatureLayer Then
Dim maptip As Border = TryCast(TryCast(layer, FeatureLayer).MapTip, Border)
Dim scv As ContentControl = TryCast(maptip.Child, ContentControl)
scv.Foreground = New SolidColorBrush(Colors.Black)
mapTipsElements.Add(layer.ID, maptip)
End If
layerCollection.Add(layer)
i += 1
Next
e.Map.Layers.Clear()
MyMap.Layers = layerCollection
End If
End Sub
Private Sub MapTipRadioButton_Checked(sender As Object , e As RoutedEventArgs)
If MyMap IsNot Nothing Then
MyInfoWindow.IsOpen = False
For Each layer As Layer In MyMap.Layers
If TypeOf layer Is FeatureLayer Then
RemoveHandler TryCast(layer, FeatureLayer).MouseLeftButtonUp, AddressOf WebMapFeatureServicePopups_MouseLeftButtonUp
End If
If mapTipsElements.ContainsKey(layer.ID) Then
TryCast(layer, FeatureLayer).MapTip = mapTipsElements(layer.ID)
End If
Next
End If
End Sub
Private Sub InfoWindowRadioButton_Unchecked(sender As Object , e As RoutedEventArgs)
For Each layer As Layer In MyMap.Layers
If TypeOf layer Is FeatureLayer Then
TryCast(layer, FeatureLayer).MapTip = Nothing
AddHandler TryCast(layer, FeatureLayer).MouseLeftButtonUp, AddressOf WebMapFeatureServicePopups_MouseLeftButtonUp
End If
Next
End Sub
Private Sub WebMapFeatureServicePopups_MouseLeftButtonUp(sender As Object , e As GraphicMouseButtonEventArgs)
Dim flayer As FeatureLayer = TryCast(sender, FeatureLayer)
Dim clickPoint As MapPoint = MyMap.ScreenToMap(e.GetPosition(MyMap))
If clickPoint IsNot lastPoint Then
If flayer.GetValue(Document.PopupTemplateProperty) IsNot Nothing Then
Dim dt As DataTemplate = TryCast(flayer.GetValue(Document.PopupTemplateProperty), DataTemplate)
MyInfoWindow.Anchor = clickPoint
MyInfoWindow.ContentTemplate = dt
MyInfoWindow.Foreground = New SolidColorBrush(Colors.Black)
MyInfoWindow.Content = e.Graphic.Attributes
MyInfoWindow.IsOpen = True
MyInfoWindow.Background = TryCast(LayoutRoot.Resources("PanelGradient" ), Brush)
lastPoint = clickPoint
End If
End If
End Sub
End Class
End Namespace
5/16/2014