This sample demonstrates how to retrieve contents from a webmap on ArcGIS Online that contains a tiled map service with pop-ups. A set of attached properties on the Document class enable access to layer properties within a webmap. This includes popup details provided as data templates via the PopupTemplates attached property. The PopupTemplates property references a dictionary of data templates, one for each feature layer in a tiled map service on which a popup was configured. Attribute values of features in layers within an ArcGIS tiled map service can be used to populate the data template. In this sample, a layer in an ArcGIS tiled map service on which pop-ups are configured is queried upon click of the map. If the query of the layer returns attributes, the attributes of the first feature are displayed in an InfoWindow whose content template is set to the data template for the popup. Note that a feature layer in a tiled map service must expose the query operation to support this functionality.
Download Sample Application
< UserControl x:Class = " ArcGISWPFSDK.WebMapTiledServicePopups "
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 " >
< esri : Map x:Name = " MyMap " MouseClick = " MyMap_MouseClick " />
< esri : InfoWindow x:Name = " MyInfoWindow "
Padding = " 2 "
CornerRadius = " 10 "
Map = " {Binding ElementName=MyMap} " >
< esri : InfoWindow.Background >
< LinearGradientBrush 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 >
</ esri : InfoWindow.Background >
</ esri : InfoWindow >
</ Grid >
</ UserControl >
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Symbols;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.WebMap;
namespace ArcGISWPFSDK
{
public partial class WebMapTiledServicePopups : UserControl
{
public WebMapTiledServicePopups()
{
InitializeComponent();
Document webMap = new Document();
webMap.GetMapCompleted += webMap_GetMapCompleted;
webMap.GetMapAsync("0e8aa0cc8dcb47d3b9a46e3c6c7c0f8f" );
}
void webMap_GetMapCompleted(object sender, GetMapCompletedEventArgs e)
{
if (e.Error == null )
{
MyMap.Extent = e.Map.Extent;
LayerCollection layerCollection = new LayerCollection();
foreach (Layer layer in e.Map.Layers)
layerCollection.Add(layer);
GraphicsLayer selectedGraphics = new GraphicsLayer()
{
RendererTakesPrecedence = false ,
ID = "MySelectionGraphicsLayer"
};
layerCollection.Add(selectedGraphics);
e.Map.Layers.Clear();
MyMap.Layers = layerCollection;
}
}
private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
{
GraphicsLayer glayer = MyMap.Layers["MySelectionGraphicsLayer" ] as GraphicsLayer;
glayer.Graphics.Clear();
MyInfoWindow.IsOpen = false ;
double mapScale = (MyMap.Resolution * 39.3700787) * 96;
ArcGISTiledMapServiceLayer alayer = null ;
DataTemplate dt = null ;
int layid = 0;
foreach (Layer layer in MyMap.Layers)
{
if (layer.GetValue(Document.PopupTemplatesProperty) != null )
{
alayer = layer as ArcGISTiledMapServiceLayer;
IDictionary<int , DataTemplate> idict = layer.GetValue(Document.PopupTemplatesProperty) as IDictionary<int , DataTemplate>;
foreach (LayerInfo linfo in alayer.Layers)
{
if (((mapScale > linfo.MaxScale // in scale range
&& mapScale < linfo.MinScale) ||
(linfo.MaxScale == 0.0 // no scale dependency
&& linfo.MinScale == 0.0) ||
(mapScale > linfo.MaxScale // minscale = 0.0 = infinity
&& linfo.MinScale == 0.0)) &&
idict.ContainsKey(linfo.ID)) // id present in dictionary
{
layid = linfo.ID;
dt = idict[linfo.ID];
break ;
}
}
}
}
if (dt != null )
{
QueryTask qt = new QueryTask(string .Format("{0}/{1}" , alayer.Url, layid));
qt.ExecuteCompleted += (s, qe) =>
{
if (qe.FeatureSet.Features.Count > 0)
{
Graphic g = qe.FeatureSet.Features[0];
MyInfoWindow.Anchor = e.MapPoint;
MyInfoWindow.ContentTemplate = dt;
MyInfoWindow.Content = g.Attributes;
MyInfoWindow.IsOpen = true ;
SolidColorBrush symbolColor = new SolidColorBrush(Colors.Cyan);
if (g.Geometry is Polygon || g.Geometry is Envelope)
{
g.Symbol = new SimpleFillSymbol()
{
BorderBrush = symbolColor,
BorderThickness = 2
};
}
else if (g.Geometry is Polyline)
{
g.Symbol = new SimpleLineSymbol()
{
Color = symbolColor
};
}
else // Point
{
g.Symbol = new SimpleMarkerSymbol()
{
Color = symbolColor,
Size = 12
};
}
glayer.Graphics.Add(g);
}
};
ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();
query.Geometry = e.MapPoint;
query.OutSpatialReference = MyMap.SpatialReference;
query.OutFields.Add("*" );
query.ReturnGeometry = true ;
qt.ExecuteAsync(query);
}
}
}
}
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Geometry
Imports ESRI.ArcGIS.Client.Symbols
Imports ESRI.ArcGIS.Client.Tasks
Imports ESRI.ArcGIS.Client.WebMap
Namespace ArcGISWPFSDK
Partial Public Class WebMapTiledServicePopups
Inherits UserControl
Public Sub New ()
InitializeComponent()
Dim webMap As New Document()
AddHandler webMap.GetMapCompleted, AddressOf webMap_GetMapCompleted
webMap.GetMapAsync("0e8aa0cc8dcb47d3b9a46e3c6c7c0f8f" )
End Sub
Private Sub webMap_GetMapCompleted(sender As Object , e As GetMapCompletedEventArgs)
If e.[Error ] Is Nothing Then
MyMap.Extent = e.Map.Extent
Dim layerCollection As New LayerCollection()
For Each layer As Layer In e.Map.Layers
layerCollection.Add(layer)
Next
Dim selectedGraphics As New GraphicsLayer() With { _
.RendererTakesPrecedence = False , _
.ID = "MySelectionGraphicsLayer" _
}
layerCollection.Add(selectedGraphics)
e.Map.Layers.Clear()
MyMap.Layers = layerCollection
End If
End Sub
Private Sub MyMap_MouseClick(sender As Object , e As ESRI.ArcGIS.Client.Map.MouseEventArgs)
Dim glayer As GraphicsLayer = TryCast(MyMap.Layers("MySelectionGraphicsLayer" ), GraphicsLayer)
glayer.Graphics.Clear()
MyInfoWindow.IsOpen = False
Dim mapScale As Double = (MyMap.Resolution * 39.3700787) * 96
Dim alayer As ArcGISTiledMapServiceLayer = Nothing
Dim dt As DataTemplate = Nothing
Dim layid As Integer = 0
For Each layer As Layer In MyMap.Layers
If layer.GetValue(Document.PopupTemplatesProperty) IsNot Nothing Then
alayer = TryCast(layer, ArcGISTiledMapServiceLayer)
Dim idict As IDictionary(Of Integer , DataTemplate) = TryCast(layer.GetValue(Document.PopupTemplatesProperty), IDictionary(Of Integer , DataTemplate))
For Each linfo As LayerInfo In alayer.Layers
' in scale range
' no scale dependency
' minscale = 0.0 = infinity
If ((mapScale > linfo.MaxScale AndAlso mapScale < linfo.MinScale) OrElse (linfo.MaxScale = 0.0 AndAlso linfo.MinScale = 0.0) OrElse (mapScale > linfo.MaxScale AndAlso linfo.MinScale = 0.0)) AndAlso idict.ContainsKey(linfo.ID) Then
' id present in dictionary
layid = linfo.ID
dt = idict(linfo.ID)
Exit For
End If
Next
End If
Next
If dt IsNot Nothing Then
Dim qt As New QueryTask(String .Format("{0}/{1}" , alayer.Url, layid))
AddHandler qt.ExecuteCompleted, Function (s, qe)
If qe.FeatureSet.Features.Count > 0 Then
Dim g As Graphic = qe.FeatureSet.Features(0)
MyInfoWindow.Anchor = e.MapPoint
MyInfoWindow.ContentTemplate = dt
MyInfoWindow.Content = g.Attributes
MyInfoWindow.IsOpen = True
Dim symbolColor As New SolidColorBrush(Colors.Cyan)
If TypeOf g.Geometry Is Polygon OrElse TypeOf g.Geometry Is Envelope Then
g.Symbol = New SimpleFillSymbol() With { _
.BorderBrush = symbolColor, _
.BorderThickness = 2 _
}
ElseIf TypeOf g.Geometry Is Polyline Then
g.Symbol = New SimpleLineSymbol() With { _
.Color = symbolColor _
}
Else
' Point
g.Symbol = New SimpleMarkerSymbol() With { _
.Color = symbolColor, _
.Size = 12 _
}
End If
glayer.Graphics.Add(g)
End If
End Function
Dim query As New ESRI.ArcGIS.Client.Tasks.Query()
query.Geometry = e.MapPoint
query.OutSpatialReference = MyMap.SpatialReference
query.OutFields.Add("*" )
query.ReturnGeometry = True
qt.ExecuteAsync(query)
End If
End Sub
End Class
End Namespace
5/16/2014