This sample demonstrates adding data in a WebMap document housed via ArcGIS Online (http://www.arcgis.com) in a Silverlight 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.Layers Property is used to obtain all of the individual Layer objects and then add them dynamically to the existing Map Control that was defined in XAML via the Map.LayerCollection.
1 <UserControl xmlns:esri="http://schemas.esri.com/arcgis/client/2009" x:Class="LoadWebmapDynamic.MainPage" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 mc:Ignorable="d" 7 d:DesignHeight="300" d:DesignWidth="400"> 8 9 <Grid x:Name="LayoutRoot" Background="White"> 10 <esri:Map x:Name="MyMap" /> 11 12 <Grid HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,15,0"> 13 <Rectangle Stroke="Gray" RadiusX="10" RadiusY="10" Fill="#77919191" Margin="0,0,0,5" > 14 <Rectangle.Effect> 15 <DropShadowEffect/> 16 </Rectangle.Effect> 17 </Rectangle> 18 <Rectangle Fill="#DDFFFFFF" Stroke="DarkGray" RadiusX="5" RadiusY="5" Margin="10" /> 19 <StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="0,20,30,20"> 20 <TextBlock FontWeight="Bold" HorizontalAlignment="Center" Text="Enter ArcGIS.com WebMap ID" /> 21 <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" > 22 <TextBlock Text="WebMap ID: " TextAlignment="Right" 23 VerticalAlignment="Center" Margin="30,0,0,0" /> 24 <TextBox x:Name="WebMapTextBox" Text="00e5e70929e14055ab686df16c842ec1" Width="250"/> 25 </StackPanel> 26 <Button x:Name="LoadWebMapButton" Content="Load WebMap" Width="100" Margin="0,5,0,10" 27 HorizontalAlignment="Center" Click="LoadWebMapButton_Click" /> 28 <TextBlock TextDecorations="underline" Text="WebMap Item Details" FontWeight="Bold" 29 Margin="30,0,0,0" />
//接下来的红色部分代码很重要,这是一种快捷的绑定属性的方法
30 <TextBlock x:Name="WebMapPropertiesTextBox" Margin="30,0,0,0" > 31 <Run FontStyle="Italic">Title: </Run> 32 <Run Text="{Binding Title}" /> 33 <LineBreak /> 34 <Run FontStyle="Italic">Snippet: </Run> 35 <Run Text="{Binding Snippet}" /> 36 <LineBreak /> 37 <Run FontStyle="Italic">Owner: </Run> 38 <Run Text="{Binding Owner}" /> 39 </TextBlock> 40 </StackPanel> 41 </Grid> 42 </Grid> 43 44 </UserControl>
1 using ESRI.ArcGIS.Client; 2 using ESRI.ArcGIS.Client.WebMap; 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Net; 7 using System.Windows; 8 using System.Windows.Controls; 9 using System.Windows.Documents; 10 using System.Windows.Input; 11 using System.Windows.Media; 12 using System.Windows.Media.Animation; 13 using System.Windows.Shapes; 14 15 namespace LoadWebmapDynamic 16 { 17 public partial class MainPage : UserControl 18 { 19 public MainPage() 20 { 21 InitializeComponent(); 22 } 23 private void LoadWebMapButton_Click(object sender, System.Windows.RoutedEventArgs e) 24 { 25 if (!string.IsNullOrEmpty(WebMapTextBox.Text)) 26 { 27 Document webMap = new Document(); 28 webMap.GetMapCompleted += (s, a) =>//这段代码非常重要很简洁,之前从未用过 29 { 30 if (a.Error != null) 31 MessageBox.Show(string.Format("Unable to load webmap. {0}", a.Error.Message)); 32 else 33 { 34 MyMap.Extent = a.Map.Extent; 35 36 LayerCollection layerCollection = new LayerCollection(); 37 foreach (Layer layer in a.Map.Layers) 38 layerCollection.Add(layer); 39 40 a.Map.Layers.Clear(); 41 MyMap.Layers = layerCollection; 42 WebMapPropertiesTextBox.DataContext = a.ItemInfo;//这是一种快捷绑定属性的方式!!! 43 } 44 }; 45 46 webMap.GetMapAsync(WebMapTextBox.Text); 47 } 48 49 } 50 } 51 }