This sample demonstrates how to retrieve contents from a webmap on ArcGIS Online that contains a dynamic 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 layer in a dynamic map service on which a popup was configured. Attribute values of features in layers within an ArcGIS dynamic map service can be used to populate the data template. In this sample, a layer in an ArcGIS dynamic 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.
Download Sample Application
< UserControl x:Class = " ArcGISWPFSDK.WebMapDynamicServicePopups "
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 " >
< esri : Map x:Name = " MyMap " MouseClick = " MyMap_MouseClick " />
< esri : InfoWindow x:Name = " MyInfoWindow "
Padding = " 2 "
CornerRadius = " 10 "
Map = " {Binding ElementName=MyMap} " Foreground = " Black " >
< 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 ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.WebMap;
namespace ArcGISWPFSDK
{
public partial class WebMapDynamicServicePopups : UserControl
{
public WebMapDynamicServicePopups()
{
InitializeComponent();
Document webMap = new Document();
webMap.GetMapCompleted += webMap_GetMapCompleted;
webMap.GetMapAsync("fd7fb514579f4422ab2698f47a7d4a46" );
}
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);
e.Map.Layers.Clear();
MyMap.Layers = layerCollection;
}
}
private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
{
MyInfoWindow.IsOpen = false ;
double mapScale = (MyMap.Resolution * 39.3700787) * 96;
ArcGISDynamicMapServiceLayer alayer = null ;
DataTemplate dt = null ;
int layid = 0;
foreach (Layer layer in MyMap.Layers)
{
if (layer.GetValue(Document.PopupTemplatesProperty) != null )
{
alayer = layer as ArcGISDynamicMapServiceLayer;
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 ;
}
};
ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();
double contractRatio = MyMap.Extent.Width / 20;
Envelope inputEnvelope = new Envelope(e.MapPoint.X - contractRatio,
e.MapPoint.Y - contractRatio,
e.MapPoint.X + contractRatio,
e.MapPoint.Y + contractRatio);
inputEnvelope.SpatialReference = MyMap.SpatialReference;
query.Geometry = inputEnvelope;
query.OutSpatialReference = MyMap.SpatialReference;
query.OutFields.Add("*" );
qt.ExecuteAsync(query);
}
}
}
}
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Geometry
Imports ESRI.ArcGIS.Client.Tasks
Imports ESRI.ArcGIS.Client.WebMap
Namespace ArcGISWPFSDK
Partial Public Class WebMapDynamicServicePopups
Inherits UserControl
Public Sub New ()
InitializeComponent()
Dim webMap As New Document()
AddHandler webMap.GetMapCompleted, AddressOf webMap_GetMapCompleted
webMap.GetMapAsync("fd7fb514579f4422ab2698f47a7d4a46" )
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
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)
MyInfoWindow.IsOpen = False
Dim mapScale As Double = (MyMap.Resolution * 39.3700787) * 96
Dim alayer As ArcGISDynamicMapServiceLayer = 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, ArcGISDynamicMapServiceLayer)
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
End If
End Function
Dim query As New ESRI.ArcGIS.Client.Tasks.Query()
Dim contractRatio As Double = MyMap.Extent.Width / 20
Dim inputEnvelope As New Envelope(e.MapPoint.X - contractRatio, e.MapPoint.Y - contractRatio, e.MapPoint.X + contractRatio, e.MapPoint.Y + contractRatio)
inputEnvelope.SpatialReference = MyMap.SpatialReference
query.Geometry = inputEnvelope
query.OutSpatialReference = MyMap.SpatialReference
query.OutFields.Add("*" )
qt.ExecuteAsync(query)
End If
End Sub
End Class
End Namespace
5/16/2014