arcgis中创建图元(Graphic)对象 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; namespace ArcGISSilverlightSDK { 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,'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], System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(gpsNMEASentence[2], System.Globalization.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], System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(gpsNMEASentence[2], System.Globalization.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], System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(lineCoords[i + 1], System.Globalization.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; PointCollectionConverter pointConverter = new PointCollectionConverter(); ESRI.ArcGIS.Client.Geometry.PointCollection pointCollection1 = pointConverter.ConvertFromString(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; } } } arcgis for silverlight 为graphiclayer 上的点添加属性信息和获取属性信息 分类: silverlight王国 2012-06-11 14:06 200人阅读 评论(0) 收藏 举报 silverlightnullexception测试存储 #region 获取所有权限视频点加载到图层 private void MonitorpointInfo() { #region 测试数据 //foreach (MapService.MonitorModel objMonitor in lisMonitor) //{ // if ((objMonitor.X != null || objMonitor.X != "") && (objMonitor.Y != null || objMonitor.Y != "")) // { // } //} //MonitorModel objM1=new MonitorModel() //{ // X = 98318.3584436682, // Y = 3498207.6618042 //}; //MonitorModel objM2 = new MonitorModel() //{ // X = 98328.3584436682, // Y = 3498207.6618042 //}; //MonitorModel objM3 = new MonitorModel() //{ // X = 98338.3584436682, // Y = 3498207.6618042 //}; //lisMonitor.Add(objM1); //lisMonitor.Add(objM2); //lisMonitor.Add(objM3); #endregion // 单个视频点 ESRI.ArcGIS.Client.Graphic graphic = null; // 为了使视频点能够随着地图比例的变化改变使用面 ESRI.ArcGIS.Client.Geometry.MapPoint mapPointVideo = null; for (int i = 0; i < lisMonitor.Count; i++) { MapService.MonitorModel objMM = lisMonitor[i]; if ((objMM.X != null && objMM.X != "") && (objMM.Y != null && objMM.Y != "")) { mapPointVideo = new MapPoint(double.Parse(objMM.X), double.Parse(objMM.Y)); graphic = new ESRI.ArcGIS.Client.Graphic() { Geometry = mapPointVideo, Symbol = this.LayoutRoot.Resources["DefaultPictureSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol }; graphic.Attributes.Add("MonitorId", objMM.MonitorId); graphic.Attributes.Add("Address", objMM.Address); graphic.Attributes.Add("GroupId", objMM.GroupId); graphic.Attributes.Add("Memo", objMM.Memo); graphic.Attributes.Add("MonitorCode", objMM.MonitorCode); graphic.Attributes.Add("MonitorGuid", objMM.MonitorGuid); graphic.Attributes.Add("MonitorIp", objMM.MonitorIp); graphic.Attributes.Add("MonitorUrl", objMM.MonitorUrl); graphic.Attributes.Add("MonitorName", objMM.MonitorName); graphic.Attributes.Add("X", objMM.X); graphic.Attributes.Add("Y", objMM.Y); graphic.Attributes.Add("Towns", objMM.Towns); graphic.Attributes.Add("MonitorPort", objMM.MonitorPort); graphic.Attributes.Add("UserCode", objMM.UserCode); graphic.Attributes.Add("PassWord", objMM.PassWord); //string strid = objgraphic.Attributes["ID"].ToString(); // 构建提示信息 FrameworkElement mapTipElement = GetMapTipInfo(objMM); if (mapTipElement != null) { graphic.MapTip = mapTipElement; graphic.MapTip.Opacity = 1; } featureLayerMonitor.Graphics.Add(graphic); } else { lisMonitor.RemoveAt(i); i--; } } // 设置其聚簇显示 FlareClusterer myflareClusterer = new FlareClusterer(); //设置背景色 myflareClusterer.FlareBackground = new SolidColorBrush(Colors.Orange); //设置前景色 myflareClusterer.FlareForeground = new SolidColorBrush() { Color = Color.FromArgb(99, 0, 0, 0) }; // 设置以像素为单位的聚集半径 myflareClusterer.Radius = 20; // 聚集的最大个数,超过则用大聚集设置Gradient myflareClusterer.MaximumFlareCount = 30; featureLayerMonitor.Clusterer = myflareClusterer; this.CurrentMap.Layers.Add(featureLayerMonitor); } #endregion /////////获取点的属性信息 case SilverlightMap.MainPage.ToolType.EditMonitor: Envelope envelope2 = e.Geometry as Envelope; MapPoint mapPoint11 = new MapPoint(envelope2.Extent.XMax, envelope2.Extent.YMax); MapPoint mapPoint12 = new MapPoint(envelope2.Extent.XMin, envelope2.Extent.YMin); GeneralTransform generalTran1 = objCurrentMap.TransformToVisual(App.Current.RootVisual); Point point11 = generalTran1.Transform(objCurrentMap.MapToScreen(mapPoint11)); Point point12 = generalTran1.Transform(objCurrentMap.MapToScreen(mapPoint12)); Rect selrect = new Rect(point11, point12); GraphicsLayer gralayer1 = objCurrentMap.Layers["FEATURELAYERMONITOR"] as GraphicsLayer; IEnumerable<Graphic> graphicList1 = gralayer1.FindGraphicsInHostCoordinates(selrect); if (graphicList1.Count() != 1 ) { MessageBox.Show("请选中一个视频点,再进行编辑!", "修改视频点", MessageBoxButton.OK); break; } foreach (Graphic objgraphic in graphicList1) { try { MapService.MonitorModel objEditMonitor = new MapService.MonitorModel(); //存储编辑视频点 Graphic objEditgraphic = objgraphic; objEditMonitor.MonitorName = objEditgraphic.Attributes["MonitorName"].ToString(); objEditMonitor.MonitorId = int.Parse(objEditgraphic.Attributes["MonitorId"].ToString()); objEditMonitor.Address = objEditgraphic.Attributes["Address"].ToString(); objEditMonitor.GroupId = int.Parse(objEditgraphic.Attributes["GroupId"].ToString()); objEditMonitor.Memo = objEditgraphic.Attributes["Memo"].ToString(); objEditMonitor.MonitorCode = objEditgraphic.Attributes["MonitorCode"].ToString(); objEditMonitor.MonitorGuid = objEditgraphic.Attributes["MonitorGuid"].ToString(); objEditMonitor.MonitorIp = objEditgraphic.Attributes["MonitorIp"].ToString(); objEditMonitor.MonitorUrl = objEditgraphic.Attributes["MonitorUrl"].ToString(); objEditMonitor.X = objEditgraphic.Attributes["X"].ToString(); objEditMonitor.Y = objEditgraphic.Attributes["Y"].ToString(); objEditMonitor.Towns = objEditgraphic.Attributes["Towns"].ToString(); objEditMonitor.MonitorPort = objEditgraphic.Attributes["MonitorPort"].ToString() == string.Empty ? 80 : int.Parse(objEditgraphic.Attributes["MonitorPort"].ToString()); objEditMonitor.UserCode = objEditgraphic.Attributes["UserCode"].ToString(); objEditMonitor.PassWord = objEditgraphic.Attributes["PassWord"].ToString(); // 弹出新增的视频点的输入框 MonitorEditInfo WinMonitorEdit = new MonitorEditInfo(objEditMonitor); WinMonitorEdit.FlwinIdentify.IsOpen = false; // 设置显示的位置 WinMonitorEdit.FlwinIdentify.InitialLeft = 100; WinMonitorEdit.FlwinIdentify.InitialTop = 70; // 添加该弹出框 objCurrentPage.LayoutRoot.Children.Add(WinMonitorEdit); WinMonitorEdit.FlwinIdentify.IsOpen = true; } catch (Exception ex) { MapService.Service1Client objWcfAgency = SilverlightMap.UtilityClass.WcfAgency.GetMapServer(); objWcfAgency.RecordExceptionToFileAsync(ex); } } objCurrentPage.CurrentToolType = SilverlightMap.MainPage.ToolType.Pan; break;