平台: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中代码:
06 | mc:Ignorable= "d" xmlns:esri= "clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client" |
07 | xmlns:esriBehaviors= "clr-namespace:ESRI.ArcGIS.Client.Behaviors;assembly=ESRI.ArcGIS.Client.Behaviors" |
08 | xmlns:i= "clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" |
09 | 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" > |
17 | <Grid x:Name= "LayoutRoot" > |
20 | <esri:Map x:Name= "myMap" Extent= "117.356,29.4949,124.299,32.567" > |
我的xmlns引用中多了 Behaviors和一些其它的,这些以后用到的时候我会讲解的
我们先看一下在地图上如何绘制点
1 | 调用就在 MainPage() 程序初始化的时候 LoadCity( "City.xml" );<br> |
1 | 记得把 City.xml放在Silverlight 项目里<br> |
2 | /// 载入地图点信息,我们从 Xml 文件中读取点信息,同样可以从外部来获取 |
4 | /// <param name="fileName">要载入的文件名称</param> |
5 | private void LoadCity( string fileName) |
02 | List<TyphoonSL.Model.CityModel> citys = new List<TyphoonSL.Model.CityModel>(); |
03 | CityModel temp = new CityModel(); |
04 | StreamResourceInfo r = Application.GetResourceStream( new Uri(fileName, UriKind.Relative)); |
05 | XmlReader reader = XmlReader.Create(r.Stream); |
09 | if (reader.AttributeCount == 5) |
11 | temp = new CityModel(); |
12 | temp.ID = Convert.ToInt32(reader.GetAttribute(0)); |
13 | temp.CityName = reader.GetAttribute(1); |
14 | temp.Level = Convert.ToInt32(reader.GetAttribute(2)); |
15 | temp.Longitude = Convert.ToDouble(reader.GetAttribute(3)); |
16 | temp.Latitude = Convert.ToDouble(reader.GetAttribute(4)); |
21 | List<Graphic> g = new List<Graphic>(); |
23 | int length = citys.Count; |
24 | for ( int i = 0; i < length; i++) |
28 | Geometry = new MapPoint(citys[i].Longitude - 0.1, citys[i].Latitude-0.1), |
29 | Symbol = new TextSymbol() { Text = citys[i].CityName, FontSize = 12, Foreground = new SolidColorBrush(Colors.Black) } |
33 | Geometry = new MapPoint(citys[i].Longitude, citys[i].Latitude), |
34 | Symbol = new SimpleMarkerSymbol() { Color = new SolidColorBrush(Colors.Orange) ,Size = 10,Style = SimpleMarkerSymbol.SimpleMarkerStyle.Circle} |
36 | g[i].MouseEnter += new MouseEventHandler(City_MouseEnter); |
37 | g[i].MouseLeave += new MouseEventHandler(City_MouseLeave); |
40 | GraphicsLayer layer = new GraphicsLayer(); |
41 | layer.ID = "CityInfo" ; |
43 | int layerCount = myMap.Layers.Count; |
44 | for ( int i = 0; i < layerCount; i++) |
46 | if (myMap.Layers[i].ID == "CityInfo" ) |
48 | myMap.Layers.RemoveAt(i); |
53 | GisMap.DrawAllLayers(myMap, new GraphicsLayer[] { layer }, g); |
54 | GisMap.AddLayersToMap(myMap, new GraphicsLayer[] { layer }); |
GisMap 类中的两个方法
07 | /// <param name="map">ArcGis 地图变量</param> |
08 | /// <param name="layers">GraphicLayer 层数组</param> |
09 | /// <param name="graphicParam">Graphic 点数组</param> |
10 | public static void DrawAllLayers(Map map, GraphicsLayer[] layers, params List<Graphic>[] graphicParam) |
15 | int length = layers.Length; |
16 | for ( int i = 0; i < length; i++) |
18 | if (layers[i] == null ) |
20 | layers[i] = new GraphicsLayer(); |
22 | DrawAllGraphics(layers[i], graphicParam[i]); |
32 | /// <param name="map">表示一张 ArcGis 地图</param> |
33 | /// <param name="layers">表示地图层的数组</param> |
34 | public static void AddLayersToMap(Map map, GraphicsLayer[] layers) |
37 | foreach (GraphicsLayer item in layers) |
City.xml 文件
01 | <?xml version= "1.0" encoding= "utf-8" ?> |
03 | <l id= "101230101" CityName= "福州" Level= "1" Longitude= "119.301389" Latitude= "25.93038" /> |
04 | <l id= "101300101" CityName= "南宁" Level= "1" Longitude= "108.272078" Latitude= "22.64439" /> |
05 | <l id= "101250101" CityName= "长沙" Level= "1" Longitude= "112.921854" Latitude= "28.07634" /> |
06 | <l id= "101260101" CityName= "贵阳" Level= "1" Longitude= "106.715264" Latitude= "26.40347" /> |
07 | <l id= "101270101" CityName= "成都" Level= "1" Longitude= "104.072857" Latitude= "30.49511" /> |
08 | <l id= "101280101" CityName= "广州" Level= "1" Longitude= "113.291693" Latitude= "22.96442" /> |
09 | <l id= "101290101" CityName= "昆明" Level= "1" Longitude= "102.704315" Latitude= "24.86215" /> |
今天写的比较忙!明天再来更新吧!以上都是简单的介绍,后面我会加一些实用的功能进去的!
感觉时间不多写的不是太好啊!郁闷~~~已经努力了!有空我多改改吧!