Create WebMap From Objects Online
This sample shows how to create a new webmap from scratch i.e. creating a new WebMap object and adding a BaseMap and OperationLayers. In this sample a webmap object is created with a base map and operationlayers including a ArcGISDynamicMapService layer, a featureService with Pop-ups and a featurecollection.
Download Sample Application<UserControl x:Class="ArcGISWPFSDK.CreateWebMapObject" 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" /> </UserControl>
using System.Windows; using System.Windows.Controls; using System.Windows.Media; using ESRI.ArcGIS.Client; using ESRI.ArcGIS.Client.WebMap; using System.Collections.Generic; using ESRI.ArcGIS.Client.Tasks; namespace ArcGISWPFSDK { public partial class CreateWebMapObject : UserControl { WebMap webmap; List<WebMapLayer> operationLayers = new List<WebMapLayer>(); BaseMap basemap; public CreateWebMapObject() { InitializeComponent(); //Define BaseMap Layer basemap = new BaseMap() { Layers = new List<WebMapLayer> { new WebMapLayer { Url = "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" } } }; //Add a ArcGISDynamicMapService operationLayers.Add(new WebMapLayer { Url = "http://serverapps10.esri.com/ArcGIS/rest/services/California/MapServer", VisibleLayers = new List<object> { 0, 1, 3, 6, 9 } }); //Define popup IList<FieldInfo> fieldinfos = new List<FieldInfo>(); fieldinfos.Add(new FieldInfo() { FieldName = "STATE_NAME", Label = "State", Visible = true }); IList<MediaInfo> mediainfos = new List<MediaInfo>(); MediaInfoValue infovalue = new MediaInfoValue(); infovalue.Fields = new string[] { "POP2000,POP2007" }; mediainfos.Add(new MediaInfo() { Type = MediaType.PieChart, Value = infovalue }); PopupInfo popup = new PopupInfo() { FieldInfos = fieldinfos, MediaInfos = mediainfos, Title = "Population Change between 2000 and 2007", }; //Add a Feature Layer with popup operationLayers.Add(new WebMapLayer { Url = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3", Mode = FeatureLayer.QueryMode.OnDemand, PopupInfo = popup }); //Perform Query to get a featureSet and add to webmap as featurecollection QueryTask qt = new QueryTask() { Url = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/EarthquakesFromLastSevenDays/MapServer/0" }; qt.ExecuteCompleted += qt_ExecuteCompleted; ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query(); query.OutFields.Add("*"); query.Where = "magnitude > 3.5"; query.ReturnGeometry = true; qt.Failed += (a, b) => { MessageBox.Show("QueryTask failed to execute:" + b.Error); }; qt.ExecuteAsync(query); } void qt_ExecuteCompleted(object sender, QueryEventArgs e) { #region Since featureset does not include layerdefinition, we would have to populate it with appropriate drawinginfo var symbol = new ESRI.ArcGIS.Client.Symbols.SimpleMarkerSymbol { Color = new SolidColorBrush(Colors.Red), Size = 8 }; var renderer = new SimpleRenderer { Symbol = symbol }; var drawingInfo = new DrawingInfo { Renderer = renderer }; var layerdef = new ESRI.ArcGIS.Client.WebMap.LayerDefinition { DrawingInfo = drawingInfo }; layerdef.AddCustomProperty("name", "Earthquakes from last 7 days"); #endregion //Add a FeatureCollection to operational layers FeatureCollection featureCollection = null; if (e.FeatureSet.Features.Count > 0) { var sublayer = new WebMapSubLayer(); sublayer.FeatureSet = e.FeatureSet; sublayer.LayerDefinition = layerdef; featureCollection = new FeatureCollection { SubLayers = new List<WebMapSubLayer> { sublayer } }; } if (featureCollection != null) operationLayers.Add(new WebMapLayer { FeatureCollection = featureCollection }); //Create a new webmap object and add base map and operational layers webmap = new WebMap() { BaseMap = basemap, OperationalLayers = operationLayers }; Document webmapdoc = new Document(); webmapdoc.GetMapCompleted += (a, b) => { if (b.Error == null) { b.Map.Extent = new ESRI.ArcGIS.Client.Geometry.Envelope(-20000000, 1100000, -3900000, 11000000); LayoutRoot.Children.Add(b.Map); } }; webmapdoc.GetMapAsync(webmap); } } }
Imports System.Windows.Controls Imports ESRI.ArcGIS.Client Imports ESRI.ArcGIS.Client.WebMap Imports System.Collections.Generic Imports System.Collections Imports ESRI.ArcGIS.Client.Tasks Imports System.Windows Namespace ArcGISWPFSDK Partial Public Class CreateWebMapObject Inherits UserControl Private webmap As WebMap Private operationLayers As New List(Of WebMapLayer)() Private basemap As BaseMap Public Sub New() InitializeComponent() 'Define BaseMap Layer basemap = New BaseMap() With {.Layers = New List(Of WebMapLayer) From { New WebMapLayer With {.Url = "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"}}} 'Add a ArcGISDynamicMapService operationLayers.Add(New WebMapLayer With {.Url = "http://serverapps10.esri.com/ArcGIS/rest/services/California/MapServer", .VisibleLayers = New List(Of Object) From {0, 1, 3, 6, 9}}) 'Define popup Dim fieldinfos As IList(Of FieldInfo) = New List(Of FieldInfo)() fieldinfos.Add(New FieldInfo() With {.FieldName = "STATE_NAME", .Label = "State", .Visible = True}) Dim mediainfos As IList(Of MediaInfo) = New List(Of MediaInfo)() Dim infovalue As New MediaInfoValue() infovalue.Fields = New String() {"POP2000,POP2007"} mediainfos.Add(New MediaInfo() With {.Type = MediaType.PieChart, .Value = infovalue}) Dim popup As New PopupInfo() With {.FieldInfos = fieldinfos, .MediaInfos = mediainfos, .Title = "Population Change between 2000 and 2007"} 'Add a Feature Layer with popup operationLayers.Add(New WebMapLayer With {.Url = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3", .Mode = FeatureLayer.QueryMode.OnDemand, .PopupInfo = popup}) 'Perform Query to get a featureSet and add to webmap as featurecollection Dim qt As New QueryTask() With {.Url = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/EarthquakesFromLastSevenDays/MapServer/0"} AddHandler qt.ExecuteCompleted, AddressOf qt_ExecuteCompleted Dim query As New ESRI.ArcGIS.Client.Tasks.Query() query.OutFields.Add("*") query.Where = "magnitude > 3.5" query.ReturnGeometry = True AddHandler qt.Failed, Sub(a, b) MessageBox.Show("QueryTask failed to execute:" & b.Error.ToString()) qt.ExecuteAsync(query) End Sub Private Sub qt_ExecuteCompleted(ByVal sender As Object, ByVal e As QueryEventArgs) ' #Region "Since featureset does not include layerdefinition, we would have to populate it with appropriate drawinginfo " Dim layerdef As New Dictionary(Of String, Object)() Dim defdictionary As New Dictionary(Of String, Object)() From {{"id", 0}, {"name", "Earthquakes from last 7 days"}} Dim renderer As New Dictionary(Of String, Object)() renderer.Add("type", "simple") renderer.Add("style", "esriSMSCircle") Dim color() As Integer = {255, 0, 0, 255} renderer.Add("color", color) renderer.Add("size", 4) Dim outlinecolor() As Integer = {0, 0, 0, 255} defdictionary.Add("drawingInfo", renderer) layerdef.Add("layerDefinition", defdictionary) ' #End Region 'Add a FeatureCollection to operational layers Dim featureCollection As FeatureCollection = Nothing If e.FeatureSet.Features.Count > 0 Then Dim sublayer = New WebMapSubLayer() sublayer.FeatureSet = e.FeatureSet sublayer.AddCustomProperty("layerDefinition", layerdef) featureCollection = New FeatureCollection With {.SubLayers = New List(Of WebMapSubLayer) From {sublayer}} End If If featureCollection IsNot Nothing Then operationLayers.Add(New WebMapLayer With {.FeatureCollection = featureCollection}) End If 'Create a new webmap object and add base map and operational layers webmap = New WebMap() With {.BaseMap = basemap, .OperationalLayers = operationLayers} Dim webmapdoc As New Document() AddHandler webmapdoc.GetMapCompleted, Sub(a, b) If b.Error Is Nothing Then b.Map.Extent = New ESRI.ArcGIS.Client.Geometry.Envelope(-20000000, 1100000, -3900000, 11000000) LayoutRoot.Children.Add(b.Map) End If End Sub webmapdoc.GetMapAsync(webmap) End Sub End Class End Namespace
Copyright © 1995-2014 Esri. All rights reserved.
5/16/2014