平台: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中代码:
- <UserControl
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d" xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
- xmlns:esriBehaviors="clr-namespace:ESRI.ArcGIS.Client.Behaviors;assembly=ESRI.ArcGIS.Client.Behaviors"
- xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
- xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
- xmlns:System="clr-namespace:System;assembly=mscorlib"
- xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
- xmlns:esriConverters="clr-namespace:ESRI.ArcGIS.Client.ValueConverters;assembly=ESRI.ArcGIS.Client"
- xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client"
- d:DesignWidth="1024" d:DesignHeight="768" x:Class="TyphoonSL.MainPage">
- <Grid x:Name="LayoutRoot">
- <!--Gis 地图-->
- <esri:Map x:Name="myMap" Extent="117.356,29.4949,124.299,32.567">
- </esri:Map>
- </Grid>
我的xmlns引用中多了 Behaviors和一些其它的,这些以后用到的时候我会讲解的
我们先看一下在地图上如何绘制点
调用就在 MainPage() 程序初始化的时候 LoadCity("City.xml");
记得把 City.xml放在Silverlight 项目里
- /// <summary>
- /// 载入地图点信息,我们从 Xml 文件中读取点信息,同样可以从外部来获取
- /// </summary>
- /// <param name="fileName">要载入的文件名称</param>
- private void LoadCity(string fileName)
- {
- // 这里这个 CityModel 自己定义吧,下面有 City.xml 那个类
- List<TyphoonSL.Model.CityModel> citys = new List<TyphoonSL.Model.CityModel>();
- CityModel temp = new CityModel();
- StreamResourceInfo r = Application.GetResourceStream(new Uri(fileName, UriKind.Relative));
- XmlReader reader = XmlReader.Create(r.Stream);
- while (reader.Read())
- {
- if (reader.AttributeCount == 5)
- {
- temp = new CityModel();
- temp.ID = Convert.ToInt32(reader.GetAttribute(0));
- temp.CityName = reader.GetAttribute(1);
- temp.Level = Convert.ToInt32(reader.GetAttribute(2));
- temp.Longitude = Convert.ToDouble(reader.GetAttribute(3));
- temp.Latitude = Convert.ToDouble(reader.GetAttribute(4));
- citys.Add(temp);
- }
- }
- List<Graphic> g = new List<Graphic>();
- int length = citys.Count;
- for (int i = 0; i < length; i++)
- {
- g.Add(new Graphic()
- {
- Geometry = new MapPoint(citys[i].Longitude - 0.1, citys[i].Latitude-0.1),
- Symbol = new TextSymbol() { Text = citys[i].CityName, FontSize = 12, Foreground = new SolidColorBrush(Colors.Black) }
- });
- g.Add(new Graphic()
- {
- Geometry = new MapPoint(citys[i].Longitude, citys[i].Latitude),
- Symbol = new SimpleMarkerSymbol() { Color = new SolidColorBrush(Colors.Orange) ,Size = 10,Style = SimpleMarkerSymbol.SimpleMarkerStyle.Circle}
- });
- g[i].MouseEnter += new MouseEventHandler(City_MouseEnter);
- g[i].MouseLeave += new MouseEventHandler(City_MouseLeave);
- }
- GraphicsLayer layer = new GraphicsLayer();
- layer.ID = "CityInfo";
- int layerCount = myMap.Layers.Count;
- for (int i = 0; i < layerCount; i++)
- {
- if (myMap.Layers[i].ID == "CityInfo")
- {
- myMap.Layers.RemoveAt(i);
- break;
- }
- }
- GisMap.DrawAllLayers(myMap, new GraphicsLayer[] { layer }, g);
- GisMap.AddLayersToMap(myMap, new GraphicsLayer[] { layer });
- }
GisMap 类中的两个方法
- /// <summary>
- /// 加载所有图层上的点
- /// 画所有点
- /// 图层和点的对应关系要正确
- /// 有几个图层就要有几个点集合
- /// </summary>
- /// <param name="map">ArcGis 地图变量</param>
- /// <param name="layers">GraphicLayer 层数组</param>
- /// <param name="graphicParam">Graphic 点数组</param>
- public static void DrawAllLayers(Map map, GraphicsLayer[] layers, params List<Graphic>[] graphicParam)
- {
- // 计算要绘制的层数并一层一层的绘制(调用动态绘制方法)
- if (layers != null)
- {
- int length = layers.Length;
- for (int i = 0; i < length; i++)
- {
- if (layers[i] == null)
- {
- layers[i] = new GraphicsLayer();
- }
- DrawAllGraphics(layers[i], graphicParam[i]);
- }
- }
- }
- /// <summary>
- /// 将图层数组全部添加到 map 中
- /// </summary>
- /// <param name="map">表示一张 ArcGis 地图</param>
- /// <param name="layers">表示地图层的数组</param>
- public static void AddLayersToMap(Map map, GraphicsLayer[] layers)
- {
- // 逐个将数据添加到当前地图中
- foreach (GraphicsLayer item in layers)
- {
- if (item != null)
- {
- map.Layers.Add(item);
- }
- }
- }
City.xml 文件
- <?xml version="1.0" encoding="utf-8" ?>
- <s>
- <l id="101230101" CityName="福州" Level="1" Longitude="119.301389" Latitude="25.93038" />
- <l id="101300101" CityName="南宁" Level="1" Longitude="108.272078" Latitude="22.64439" />
- <l id="101250101" CityName="长沙" Level="1" Longitude="112.921854" Latitude="28.07634" />
- <l id="101260101" CityName="贵阳" Level="1" Longitude="106.715264" Latitude="26.40347" />
- <l id="101270101" CityName="成都" Level="1" Longitude="104.072857" Latitude="30.49511" />
- <l id="101280101" CityName="广州" Level="1" Longitude="113.291693" Latitude="22.96442" />
- <l id="101290101" CityName="昆明" Level="1" Longitude="102.704315" Latitude="24.86215" />
- </s>
今天写的比较忙!明天再来更新吧!以上都是简单的介绍,后面我会加一些实用的功能进去的!
感觉时间不多写的不是太好啊!郁闷~~~已经努力了!有空我多改改吧!
转自:http://www.cnblogs.com/Royal_WH/archive/2010/11/02/1867366.html