Add using code Online
This sample demonstrates adding graphics to the map using code. Simple marker symbols, picture marker symbols, simple line symbols, and simple fill symbols are shown.
Download Sample Application<UserControl x:Class="ArcGISWPFSDK.AddGraphics" 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"> <Grid.Resources> <esri:SimpleMarkerSymbol x:Key="RedMarkerSymbol" Color="Red" Size="12" Style="Circle" /> <esri:SimpleMarkerSymbol x:Key="BlackMarkerSymbol" Color="Black" Size="14" Style="Diamond" /> <esri:PictureMarkerSymbol x:Key="GlobePictureSymbol" OffsetX="8" OffsetY="8" Source="/Assets/Images/globe-16x16.png" /> <esri:SimpleLineSymbol x:Key="DefaultLineSymbol" Color="Green" Style="DashDot" Width="4" /> <esri:SimpleFillSymbol x:Key="DefaultFillSymbol" Fill="Green" BorderBrush="Blue" BorderThickness="3" /> </Grid.Resources> <esri:Map x:Name="MyMap" Background="White" WrapAround="True"> <esri:ArcGISTiledMapServiceLayer ID="PhysicalTiledLayer" Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"/> <esri:GraphicsLayer ID="MyGraphicsLayer" /> </esri:Map> </Grid> </UserControl>
using System; using System.Collections.Generic; using System.Runtime.Serialization; using System.Windows.Controls; using System.Windows.Media; using ESRI.ArcGIS.Client; using ESRI.ArcGIS.Client.Geometry; using ESRI.ArcGIS.Client.Symbols; using System.Globalization; namespace ArcGISWPFSDK { public partial class AddGraphics : UserControl { private static ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator(); public AddGraphics() { InitializeComponent(); AddMarkerGraphics(); AddPictureMarkerAndTextGraphics(); AddLineGraphics(); AddPolygonGraphics(); } void AddMarkerGraphics() { string jsonCoordinateString = "{\"Coordinates\":[{\"X\":13.1,\"Y\":55.59},{\"X\":72.83,\"Y\":18.97},{\"X\":55.43,\"Y\":34.3}]}"; CustomCoordinateList coordinateList = DeserializeJson<CustomCoordinateList>(jsonCoordinateString); GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer; for (int i = 0; i < coordinateList.Coordinates.Count; i++) { Graphic graphic = new Graphic() { Geometry = mercator.FromGeographic(new MapPoint(coordinateList.Coordinates[i].X, coordinateList.Coordinates[i].Y)), Symbol = i > 0 ? LayoutRoot.Resources["RedMarkerSymbol"] as Symbol : LayoutRoot.Resources["BlackMarkerSymbol"] as Symbol }; graphicsLayer.Graphics.Add(graphic); } } private void AddPictureMarkerAndTextGraphics() { string gpsNMEASentences = "$GPGGA, 92204.9, -35.6334, N, -60.2343, W, 1, 04, 2.4, 25.7, M,,,,*75\r\n" + "$GPGGA, 92510.5, -49.9334, N, -65.2131, W, 1, 04, 2.6, 1.7, M,,,,*75\r\n"; string[] gpsNMEASentenceArray = gpsNMEASentences.Split('\n'); GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer; for (int i = 0; i < gpsNMEASentenceArray.Length - 1; i++) { string[] gpsNMEASentence = gpsNMEASentenceArray[i].Split(','); Graphic graphic = new Graphic() { Geometry = mercator.FromGeographic(new MapPoint( Convert.ToDouble(gpsNMEASentence[4], CultureInfo.InvariantCulture), Convert.ToDouble(gpsNMEASentence[2], CultureInfo.InvariantCulture))), Symbol = LayoutRoot.Resources["GlobePictureSymbol"] as Symbol }; graphicsLayer.Graphics.Add(graphic); TextSymbol textSymbol = new TextSymbol() { FontFamily = new System.Windows.Media.FontFamily("Arial"), Foreground = new System.Windows.Media.SolidColorBrush(Colors.Black), FontSize = 10, Text = gpsNMEASentence[9] }; Graphic graphicText = new Graphic() { Geometry = mercator.FromGeographic(new MapPoint( Convert.ToDouble(gpsNMEASentence[4], CultureInfo.InvariantCulture), Convert.ToDouble(gpsNMEASentence[2], CultureInfo.InvariantCulture))), Symbol = textSymbol }; graphicsLayer.Graphics.Add(graphicText); } } private void AddLineGraphics() { string geoRSSLine = @"<?xml version='1.0' encoding='utf-8'?> <feed xmlns='http://www.w3.org/2005/Atom' xmlns:georss='http://www.georss.org/georss'> <georss:line>-118.169, 34.016, -104.941, 39.7072, -96.724, 32.732</georss:line> <georss:line>-28.69, 14.16, -14.91, 23.702, -1.74, 13.72</georss:line> </feed>"; List<ESRI.ArcGIS.Client.Geometry.Polyline> polylineList = new List<ESRI.ArcGIS.Client.Geometry.Polyline>(); using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(new System.IO.StringReader(geoRSSLine))) { while (xmlReader.Read()) { switch (xmlReader.NodeType) { case System.Xml.XmlNodeType.Element: string nodeName = xmlReader.Name; if (nodeName == "georss:line") { string lineString = xmlReader.ReadElementContentAsString(); string[] lineCoords = lineString.Split(','); ESRI.ArcGIS.Client.Geometry.PointCollection pointCollection = new ESRI.ArcGIS.Client.Geometry.PointCollection(); for (int i = 0; i < lineCoords.Length; i += 2) { pointCollection.Add(new MapPoint( Convert.ToDouble(lineCoords[i], CultureInfo.InvariantCulture), Convert.ToDouble(lineCoords[i + 1], CultureInfo.InvariantCulture))); } ESRI.ArcGIS.Client.Geometry.Polyline polyline = new ESRI.ArcGIS.Client.Geometry.Polyline(); polyline.Paths.Add(pointCollection); polylineList.Add(polyline); } break; } } } GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer; foreach (ESRI.ArcGIS.Client.Geometry.Polyline polyline in polylineList) { Graphic graphic = new Graphic() { Symbol = LayoutRoot.Resources["DefaultLineSymbol"] as Symbol, Geometry = mercator.FromGeographic(polyline) }; graphicsLayer.Graphics.Add(graphic); } } private void AddPolygonGraphics() { string coordinateString1 = "130,5.59 118.42,3.92 117.3,23.3 143.2,22.9 130,5.59"; GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer; ESRI.ArcGIS.Client.Geometry.PointCollectionConverter pointConverter = new ESRI.ArcGIS.Client.Geometry.PointCollectionConverter(); ESRI.ArcGIS.Client.Geometry.PointCollection pointCollection1 = pointConverter.ConvertFromString(null, CultureInfo.InvariantCulture, coordinateString1) as ESRI.ArcGIS.Client.Geometry.PointCollection; ESRI.ArcGIS.Client.Geometry.Polygon polygon1 = new ESRI.ArcGIS.Client.Geometry.Polygon(); polygon1.Rings.Add(pointCollection1); Graphic graphic = new Graphic() { Geometry = mercator.FromGeographic(polygon1), Symbol = LayoutRoot.Resources["DefaultFillSymbol"] as Symbol }; graphicsLayer.Graphics.Add(graphic); } internal static T DeserializeJson<T>(string json) { T objectInstance = Activator.CreateInstance<T>(); System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(System.Text.Encoding.Unicode.GetBytes(json)); System.Runtime.Serialization.Json.DataContractJsonSerializer jsonSerializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(objectInstance.GetType()); objectInstance = (T)jsonSerializer.ReadObject(memoryStream); memoryStream.Close(); return objectInstance; } } [DataContract] public class CustomCoordinateList { [DataMember] public List<CustomCoordinate> Coordinates = new List<CustomCoordinate>(); } [DataContract] public class CustomCoordinate { public CustomCoordinate() { } public CustomCoordinate(double x, double y) { this.X = x; this.Y = y; } [DataMember] public double X { get; set; } [DataMember] public double Y { get; set; } } }
Imports System.Collections.Generic Imports System.Runtime.Serialization Imports System.Windows.Controls Imports System.Windows.Media Imports ESRI.ArcGIS.Client Imports ESRI.ArcGIS.Client.Geometry Imports ESRI.ArcGIS.Client.Symbols Imports System.Globalization Namespace ArcGISWPFSDK Partial Public Class AddGraphics Inherits UserControl Private Shared mercator As New ESRI.ArcGIS.Client.Projection.WebMercator() Public Sub New() InitializeComponent() AddMarkerGraphics() AddPictureMarkerAndTextGraphics() AddLineGraphics() AddPolygonGraphics() End Sub Private Sub AddMarkerGraphics() Dim jsonCoordinateString As String = "{""Coordinates"":[{""X"":13.1,""Y"":55.59},{""X"":72.83,""Y"":18.97},{""X"":55.43,""Y"":34.3}]}" Dim coordinateList As CustomCoordinateList = DeserializeJson(Of CustomCoordinateList)(jsonCoordinateString) Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer) For i As Integer = 0 To coordinateList.Coordinates.Count - 1 Dim graphic As New Graphic() With { _ .Geometry = mercator.FromGeographic( _ New MapPoint( _ coordinateList.Coordinates(i).X, _ coordinateList.Coordinates(i).Y)), _ .Symbol = If(i > 0, TryCast(LayoutRoot.Resources("RedMarkerSymbol"), Symbol), TryCast(LayoutRoot.Resources("BlackMarkerSymbol"), Symbol)) _ } graphicsLayer.Graphics.Add(graphic) Next End Sub Private Sub AddPictureMarkerAndTextGraphics() Dim gpsNMEASentences As String = "$GPGGA, 92204.9, -35.6334, N, -60.2343, W, 1, 04, 2.4, 25.7, M,,,,*75" & vbCr & vbLf & "$GPGGA, 92510.5, -49.9334, N, -65.2131, W, 1, 04, 2.6, 1.7, M,,,,*75" & vbCr & vbLf Dim gpsNMEASentenceArray As String() = gpsNMEASentences.Split(ControlChars.Lf) Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer) For i As Integer = 0 To gpsNMEASentenceArray.Length - 2 Dim gpsNMEASentence As String() = gpsNMEASentenceArray(i).Split(","c) Dim graphic As New Graphic() With { _ .Geometry = mercator.FromGeographic( _ New MapPoint( _ Convert.ToDouble(gpsNMEASentence(4), CultureInfo.InvariantCulture), _ Convert.ToDouble(gpsNMEASentence(2), CultureInfo.InvariantCulture))), _ .Symbol = TryCast(LayoutRoot.Resources("GlobePictureSymbol"), Symbol) _ } graphicsLayer.Graphics.Add(graphic) Dim textSymbol As New TextSymbol() With { _ .FontFamily = New System.Windows.Media.FontFamily("Arial"), _ .Foreground = New System.Windows.Media.SolidColorBrush(Colors.Black), _ .FontSize = 10, _ .Text = gpsNMEASentence(9) _ } Dim graphicText As New Graphic() With { _ .Geometry = mercator.FromGeographic( _ New MapPoint( _ Convert.ToDouble(gpsNMEASentence(4), CultureInfo.InvariantCulture), _ Convert.ToDouble(gpsNMEASentence(2), CultureInfo.InvariantCulture))), _ .Symbol = textSymbol _ } graphicsLayer.Graphics.Add(graphicText) Next End Sub Private Sub AddLineGraphics() Dim geoRSSLine As String = "<?xml version='1.0' encoding='utf-8'?>" & vbCr & vbLf & " <feed xmlns='http://www.w3.org/2005/Atom' xmlns:georss='http://www.georss.org/georss'>" & vbCr & vbLf & " <georss:line>-118.169, 34.016, -104.941, 39.7072, -96.724, 32.732</georss:line>" & vbCr & vbLf & " <georss:line>-28.69, 14.16, -14.91, 23.702, -1.74, 13.72</georss:line>" & vbCr & vbLf & " </feed>" Dim polylineList As New List(Of ESRI.ArcGIS.Client.Geometry.Polyline)() Using xmlReader As System.Xml.XmlReader = System.Xml.XmlReader.Create(New System.IO.StringReader(geoRSSLine)) While xmlReader.Read() Select Case xmlReader.NodeType Case System.Xml.XmlNodeType.Element Dim nodeName As String = xmlReader.Name If nodeName = "georss:line" Then Dim lineString As String = xmlReader.ReadElementContentAsString() Dim lineCoords As String() = lineString.Split(","c) Dim pointCollection As New ESRI.ArcGIS.Client.Geometry.PointCollection() For i As Integer = 0 To lineCoords.Length - 1 Step 2 pointCollection.Add( _ New MapPoint(Convert.ToDouble(lineCoords(i), CultureInfo.InvariantCulture), _ Convert.ToDouble(lineCoords(i + 1), CultureInfo.InvariantCulture))) Next Dim polyline As New ESRI.ArcGIS.Client.Geometry.Polyline() polyline.Paths.Add(pointCollection) polylineList.Add(polyline) End If Exit Select End Select End While End Using Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer) For Each polyline As ESRI.ArcGIS.Client.Geometry.Polyline In polylineList Dim graphic As New Graphic() With { _ .Symbol = TryCast(LayoutRoot.Resources("DefaultLineSymbol"), Symbol), _ .Geometry = mercator.FromGeographic(polyline) _ } graphicsLayer.Graphics.Add(graphic) Next End Sub Private Sub AddPolygonGraphics() Dim coordinateString1 As String = "130,5.59 118.42,3.92 117.3,23.3 143.2,22.9 130,5.59" Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer) Dim pointConverter As New ESRI.ArcGIS.Client.Geometry.PointCollectionConverter() Dim pointCollection1 As ESRI.ArcGIS.Client.Geometry.PointCollection = _ TryCast(pointConverter.ConvertFromString(Nothing, CultureInfo.InvariantCulture, coordinateString1), ESRI.ArcGIS.Client.Geometry.PointCollection) Dim polygon1 As New ESRI.ArcGIS.Client.Geometry.Polygon() polygon1.Rings.Add(pointCollection1) Dim graphic As New Graphic() With { _ .Geometry = mercator.FromGeographic(polygon1), _ .Symbol = TryCast(LayoutRoot.Resources("DefaultFillSymbol"), Symbol) _ } graphicsLayer.Graphics.Add(graphic) End Sub Friend Shared Function DeserializeJson(Of T)(json As String) As T Dim objectInstance As T = Activator.CreateInstance(Of T)() Dim memoryStream As New System.IO.MemoryStream(System.Text.Encoding.Unicode.GetBytes(json)) Dim jsonSerializer As New System.Runtime.Serialization.Json.DataContractJsonSerializer(objectInstance.[GetType]()) objectInstance = DirectCast(jsonSerializer.ReadObject(memoryStream), T) memoryStream.Close() Return objectInstance End Function End Class <DataContract()> _ Public Class CustomCoordinateList <DataMember()> _ Public Coordinates As New List(Of CustomCoordinate)() End Class <DataContract()> _ Public Class CustomCoordinate Public Sub New() End Sub Public Sub New(x As Double, y As Double) Me.X = x Me.Y = y End Sub <DataMember()> _ Public Property X() As Double Get Return m_X End Get Set(value As Double) m_X = value End Set End Property Private m_X As Double <DataMember()> _ Public Property Y() As Double Get Return m_Y End Get Set(value As Double) m_Y = value End Set End Property Private m_Y As Double End Class End Namespace
Copyright © 1995-2014 Esri. All rights reserved.
5/16/2014