This sample demonstrates adding data in a WebMap document housed via ArcGIS Online (http://www.arcgis.com) in a WPF application. The WebMap.GetMapAsync Method call takes a Globally Unique Identifier (GUID) for the WebMap service provided by ArcGIS Online to obtain the geographic data layers. The WebMap’s GetMapCompletedEventArgs.Map Property is used to add a Map Class to the WPF application for visual display. Since some of the geographic data layers used by the WebMap document consist of data provided by Microsoft Bing Map services, setting the WebMap.BingToken Property is required.
Download Sample Application
< UserControl x:Class = " ArcGISWPFSDK.LoadWebMapWithBing "
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 = " #FFE3E3E3 " >
< 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 >
< Style x:Key = " Link_Button " TargetType = " Button " >
< Setter Property = " Template " >
< Setter.Value >
< ControlTemplate TargetType = " Button " >
< TextBlock TextDecorations = " Underline " >
< ContentPresenter />
</ TextBlock >
</ ControlTemplate >
</ Setter.Value >
</ Setter >
< Setter Property = " Foreground " Value = " Blue " />
< Style.Triggers >
< Trigger Property = " IsMouseOver " Value = " true " >
< Setter Property = " Foreground " Value = " Red " />
</ Trigger >
</ Style.Triggers >
</ Style >
</ Grid.Resources >
< Grid Name = " BingKeyGrid " HorizontalAlignment = " Right " VerticalAlignment = " Top " Margin = " 0,15,15,0 " >
< Rectangle Stroke = " Gray " RadiusX = " 10 " RadiusY = " 10 " Fill = " {StaticResource PanelGradient} " Margin = " 0,0,0,5 " >
< Rectangle.Effect >
< DropShadowEffect />
</ Rectangle.Effect >
</ Rectangle >
< Rectangle Fill = " #DDFFFFFF " Stroke = " DarkGray " RadiusX = " 5 " RadiusY = " 5 " Margin = " 10 " />
< StackPanel Orientation = " Vertical " HorizontalAlignment = " Center " Margin = " 20,20,20,20 " >
< StackPanel Orientation = " Horizontal " HorizontalAlignment = " Center " Margin = " 2 " >
< TextBlock FontWeight = " Bold " HorizontalAlignment = " Center " Text = " Enter Bing Key " Margin = " 2 " Foreground = " Black " />
< Button Style = " {StaticResource Link_Button} " Margin = " 2 " Content = " (Get Bing Key) " Click = " Button_Click " />
</ StackPanel >
< TextBox x:Name = " BingKeyTextBox " Width = " 250 " HorizontalAlignment = " Right " Margin = " 0,2,0,2 "
TextChanged = " BingKeyTextBox_TextChanged " />
< TextBlock Name = " InvalidBingKeyTextBlock " Text = " Invalid Key " Foreground = " Red "
Margin = " 0,2,0,2 " HorizontalAlignment = " Center " Visibility = " Collapsed " />
< Button Name = " LoadMapButton " Content = " Load Map " Width = " 100 " Margin = " 0,5,0,10 " IsEnabled = " False "
HorizontalAlignment = " Center " Click = " LoadMapButton_Click " />
</ StackPanel >
</ Grid >
</ Grid >
</ UserControl >
using System;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client.WebMap;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Net;
using System.Diagnostics;
namespace ArcGISWPFSDK
{
public partial class LoadWebMapWithBing : UserControl
{
public LoadWebMapWithBing()
{
InitializeComponent();
}
private void BingKeyTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if ((sender as TextBox).Text.Length >= 64)
LoadMapButton.IsEnabled = true ;
else
LoadMapButton.IsEnabled = false ;
}
private void LoadMapButton_Click(object sender, RoutedEventArgs e)
{
WebClient webClient = new WebClient();
string uri = string .Format("http://dev.virtualearth.net/REST/v1/Imagery/Metadata/Aerial?supressStatus=true&key={0}" , BingKeyTextBox.Text);
webClient.OpenReadCompleted += (s, a) =>
{
if (a.Error == null )
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof (BingAuthentication));
BingAuthentication bingAuthentication = serializer.ReadObject(a.Result) as BingAuthentication;
a.Result.Close();
string authenticationResult = bingAuthentication.AuthenticationResultCode.ToString();
BingKeyGrid.Visibility = System.Windows.Visibility.Collapsed;
InvalidBingKeyTextBlock.Visibility = System.Windows.Visibility.Collapsed;
if (authenticationResult == "ValidCredentials" )
{
Document webMap = new Document();
webMap.BingToken = BingKeyTextBox.Text;
webMap.GetMapCompleted += (s1, e1) =>
{
if (e1.Error == null )
LayoutRoot.Children.Add(e1.Map);
};
webMap.GetMapAsync("75e222ec54b244a5b73aeef40ce76adc" );
}
else InvalidBingKeyTextBlock.Visibility = System.Windows.Visibility.Visible;
}
else InvalidBingKeyTextBlock.Visibility = System.Windows.Visibility.Visible;
};
webClient.OpenReadAsync(new System.Uri(uri));
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Process.Start("IExplore.exe" , "https://www.bingmapsportal.com" );
}
[DataContract]
public class BingAuthentication
{
[DataMember(Name = "authenticationResultCode" )]
public string AuthenticationResultCode { get ; set ; }
}
}
}
Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client.WebMap
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Json
Imports System.Net
Imports System.Diagnostics
Namespace ArcGISWPFSDK
Partial Public Class LoadWebMapWithBing
Inherits UserControl
Public Sub New ()
InitializeComponent()
End Sub
Private Sub BingKeyTextBox_TextChanged(ByVal sender As Object , ByVal e As TextChangedEventArgs)
If (TryCast(sender, TextBox)).Text.Length >= 64 Then
LoadMapButton.IsEnabled = True
Else
LoadMapButton.IsEnabled = False
End If
End Sub
Private Sub LoadMapButton_Click(ByVal sender As Object , ByVal e As RoutedEventArgs)
Dim webClient As New WebClient()
Dim uri As String = String .Format("http://dev.virtualearth.net/REST/v1/Imagery/Metadata/Aerial?supressStatus=true&key={0}" , BingKeyTextBox.Text)
AddHandler webClient.OpenReadCompleted, Sub (s, a)
If a.Error Is Nothing Then
Dim serializer As New DataContractJsonSerializer(GetType (BingAuthentication))
Dim bingAuthentication As BingAuthentication = TryCast(serializer.ReadObject(a.Result), BingAuthentication)
a.Result.Close()
Dim authenticationResult As String = bingAuthentication.AuthenticationResultCode.ToString()
BingKeyGrid.Visibility = System.Windows.Visibility.Collapsed
InvalidBingKeyTextBlock.Visibility = System.Windows.Visibility.Collapsed
If authenticationResult = "ValidCredentials" Then
Dim webMap As New Document()
webMap.BingToken = BingKeyTextBox.Text
AddHandler webMap.GetMapCompleted, Sub (s1, e1)
If e1.Error Is Nothing Then
LayoutRoot.Children.Add(e1.Map)
End If
End Sub
webMap.GetMapAsync("75e222ec54b244a5b73aeef40ce76adc" )
Else
InvalidBingKeyTextBlock.Visibility = System.Windows.Visibility.Visible
End If
Else
InvalidBingKeyTextBlock.Visibility = System.Windows.Visibility.Visible
End If
End Sub
webClient.OpenReadAsync(New System.Uri(uri))
End Sub
Private Sub Button_Click(ByVal sender As Object , ByVal e As RoutedEventArgs)
Process.Start("IExplore.exe" , "https://www.bingmapsportal.com" )
End Sub
<DataContract()>
Public Class BingAuthentication
<DataMember(Name:="authenticationResultCode" )>
Public Property AuthenticationResultCode() As String
End Class
End Class
End Namespace
5/16/2014