ArcGis For Silverlight API,地图显示Gis,绘制点,线,绘制图等(一)

来源:互联网 发布:淘宝拍单免费送衣服 编辑:程序博客网 时间:2024/05/06 10:58

平台:Vs 2010,Blend 4,Silverlight 4

调用API: ArcGis for Silverligth API(ESRI.ArcGIS.Client)

 

前言:本想只写一篇知识性的简单介绍下 ArcGis API,后来发觉程序做的比较复杂,不是一两篇能搞定的为了让大家能更深入的了解 ArcGis API 干脆写一个连载长篇的!写的不好请大家批评指正!

 

好了不说废话少说了!国际惯例先上图吧!图上实现的功能我会在后面章节中讲解!

 

 

项目准备:ArcGis API(自己找地址吧),我用的ESRI.ArcGIS.Client是1.1.0.97的版本。其它版本也应该差不多!

 

好了我们先建个网站项目,然后建个silverlight项目,把ArcGis Api 添加到项目中

 

我们先把地图加到silverlight中


Xmal中代码:


  1. <UserControl 
  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" xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client" 
  7.     xmlns:esriBehaviors="clr-namespace:ESRI.ArcGIS.Client.Behaviors;assembly=ESRI.ArcGIS.Client.Behaviors" 
  8.     xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
  9.     xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" 
  10.     xmlns:System="clr-namespace:System;assembly=mscorlib" 
  11.     xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" 
  12.     xmlns:esriConverters="clr-namespace:ESRI.ArcGIS.Client.ValueConverters;assembly=ESRI.ArcGIS.Client" 
  13.     xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client" 
  14.     d:DesignWidth="1024" d:DesignHeight="768" x:Class="TyphoonSL.MainPage"> 
  15.  
  16.  
  17. <Grid x:Name="LayoutRoot"> 
  18.           
  19.         <!--Gis 地图--> 
  20.         <esri:Map x:Name="myMap" Extent="117.356,29.4949,124.299,32.567"> 
  21.         </esri:Map> 
  22.  
  23. </Grid> 
  24.          

 

 我的xmlns引用中多了 Behaviors和一些其它的,这些以后用到的时候我会讲解的


我们先看一下在地图上如何绘制点

调用就在 MainPage() 程序初始化的时候 LoadCity("City.xml");

记得把 City.xml放在Silverlight 项目里

  1.    /// <summary> 
  2.         /// 载入地图点信息,我们从 Xml 文件中读取点信息,同样可以从外部来获取  
  3.         /// </summary> 
  4.         /// <param name="fileName">要载入的文件名称</param> 
  5.         private void LoadCity(string fileName)  
  6.         {  
  7.       // 这里这个 CityModel 自己定义吧,下面有 City.xml 那个类  
  8.             List<TyphoonSL.Model.CityModel> citys = new List<TyphoonSL.Model.CityModel>();  
  9.             CityModel temp = new CityModel();  
  10.             StreamResourceInfo r = Application.GetResourceStream(new Uri(fileName, UriKind.Relative));  
  11.             XmlReader reader = XmlReader.Create(r.Stream);  
  12.  
  13.             while (reader.Read())  
  14.             {  
  15.                 if (reader.AttributeCount == 5)  
  16.                 {  
  17.                     temp = new CityModel();  
  18.                     temp.ID = Convert.ToInt32(reader.GetAttribute(0));  
  19.                     temp.CityName = reader.GetAttribute(1);  
  20.                     temp.Level = Convert.ToInt32(reader.GetAttribute(2));  
  21.                     temp.Longitude = Convert.ToDouble(reader.GetAttribute(3));  
  22.                     temp.Latitude = Convert.ToDouble(reader.GetAttribute(4));  
  23.                     citys.Add(temp);  
  24.                 }  
  25.             }  
  26.  
  27.             List<Graphic> g = new List<Graphic>();  
  28.  
  29.             int length = citys.Count;  
  30.             for (int i = 0; i < length; i++)  
  31.             {  
  32.                 g.Add(new Graphic()  
  33.                 {  
  34.                     Geometry = new MapPoint(citys[i].Longitude - 0.1, citys[i].Latitude-0.1),  
  35.                     Symbol = new TextSymbol() { Text = citys[i].CityName, FontSize = 12Foreground = new SolidColorBrush(Colors.Black) }  
  36.                 });  
  37.                 g.Add(new Graphic()   
  38.                 {  
  39.                     Geometry = new MapPoint(citys[i].Longitude, citys[i].Latitude),  
  40.                     Symbol = new SimpleMarkerSymbol() { Color = new SolidColorBrush(Colors.Orange) ,Size = 10,Style = SimpleMarkerSymbol.SimpleMarkerStyle.Circle}  
  41.                 });  
  42.                 g[i].MouseEnter += new MouseEventHandler(City_MouseEnter);  
  43.                 g[i].MouseLeave += new MouseEventHandler(City_MouseLeave);  
  44.             }  
  45.  
  46.             GraphicsLayer layer = new GraphicsLayer();  
  47.             layer.ID = "CityInfo";  
  48.  
  49.             int layerCount = myMap.Layers.Count;  
  50.             for (int i = 0; i < layerCount; i++)  
  51.             {  
  52.                 if (myMap.Layers[i].ID == "CityInfo")  
  53.                 {  
  54.                     myMap.Layers.RemoveAt(i);  
  55.                     break;  
  56.                 }  
  57.             }  
  58.  
  59.             GisMap.DrawAllLayers(myMap, new GraphicsLayer[] { layer }, g);  
  60.             GisMap.AddLayersToMap(myMap, new GraphicsLayer[] { layer });  
  61.  
  62.         } 

 

GisMap 类中的两个方法

 

  1.    /// <summary>  
  2.         /// 加载所有图层上的点  
  3.         /// 画所有点  
  4.         /// 图层和点的对应关系要正确  
  5.         /// 有几个图层就要有几个点集合  
  6.         /// </summary>  
  7.         /// <param name="map">ArcGis 地图变量</param>  
  8.         /// <param name="layers">GraphicLayer 层数组</param>  
  9.         /// <param name="graphicParam">Graphic 点数组</param>  
  10.         public static void DrawAllLayers(Map map, GraphicsLayer[] layers, params List<Graphic>[] graphicParam)  
  11.         {  
  12.             // 计算要绘制的层数并一层一层的绘制(调用动态绘制方法)  
  13.             if (layers != null)  
  14.             {  
  15.                 int length = layers.Length;  
  16.                 for (int i = 0; i < length; i++)  
  17.                 {  
  18.                     if (layers[i] == null)  
  19.                     {  
  20.                         layers[i] = new GraphicsLayer();  
  21.                     }  
  22.                     DrawAllGraphics(layers[i], graphicParam[i]);  
  23.                 }  
  24.             }  
  25.         }  
  26.  
  27.  
  28.  
  29.    /// <summary>  
  30.         /// 将图层数组全部添加到 map 中  
  31.         /// </summary>  
  32.         /// <param name="map">表示一张 ArcGis 地图</param>  
  33.         /// <param name="layers">表示地图层的数组</param>  
  34.         public static void AddLayersToMap(Map map, GraphicsLayer[] layers)  
  35.         {  
  36.             // 逐个将数据添加到当前地图中  
  37.             foreach (GraphicsLayer item in layers)  
  38.             {  
  39.                 if (item != null)  
  40.                 {  
  41.                     map.Layers.Add(item);  
  42.                 }  
  43.             }  
  44.         } 

 

City.xml 文件

  1. <?xml version="1.0" encoding="utf-8" ?> 
  2. <s> 
  3.   <l id="101230101" CityName="福州" Level="1" Longitude="119.301389" Latitude="25.93038" /> 
  4.   <l id="101300101" CityName="南宁" Level="1" Longitude="108.272078" Latitude="22.64439" /> 
  5.   <l id="101250101" CityName="长沙" Level="1" Longitude="112.921854" Latitude="28.07634" /> 
  6.   <l id="101260101" CityName="贵阳" Level="1" Longitude="106.715264" Latitude="26.40347" /> 
  7.   <l id="101270101" CityName="成都" Level="1" Longitude="104.072857" Latitude="30.49511" /> 
  8.   <l id="101280101" CityName="广州" Level="1" Longitude="113.291693" Latitude="22.96442" /> 
  9.   <l id="101290101" CityName="昆明" Level="1" Longitude="102.704315" Latitude="24.86215" /> 
  10. </s> 
 

今天写的比较忙!明天再来更新吧!以上都是简单的介绍,后面我会加一些实用的功能进去的!

 

感觉时间不多写的不是太好啊!郁闷~~~已经努力了!有空我多改改吧!

 

转自:http://www.cnblogs.com/Royal_WH/archive/2010/11/02/1867366.html

原创粉丝点击