ArcGIS API for Silverlight开发入门(8):在程序中使用Virtual Earth的服务

简介:




SilverlightAPI中还包括了一个ESRI.ArcGIS.VirtualEarth.dll类库,让我们可以方便的访问到老东家的VirtualEarth服务。目前SilverlightAPI中提供的VirtualEarth服务有三种:Map,Geocode和Routing,不过一看就知道后两种服务对于国内的数据来说又无缘了。
        直接看如何使用它的Map服务获取地图数据吧。同前,新建一个Silverlight工程,添加ESRI.ArcGIS.dll和ESRI.ArcGIS.VirtualEarth.dll的引用,引入xml命名空间,在xaml里面这样写:

  1. <esri:Map x:Name="Map1" Loaded="Map1_Loaded">
  2.             <esri:Map.Layers>
  3.                 <esriVE:TileLayer ID="VELayer" LayerStyle="AerialWithLabels" ServerType="Staging"/>
  4.             </esri:Map.Layers>
  5.         </esri:Map>
复制代码
可以看出,和添加其他图层基本是一样的。SIlverlightAPI中针对VE地图的图层类型是TileLayer,LayerStyle有三种:Road,Aerial和AerialWithLabels,分别对应矢量图,影像图和带街道标注的影像图。ServerType就比较特殊了,有两种:Staging和Production,分别对应访问VE服务的账户类别,前者是免费的,后者是收费的。如果你此时运行程序的话,那是看不到地图的,因为TileLayer还有个关键的token属性没有设置。
        VE的服务那是相当安全,每次访问VE的服务,都要提供一个token(一个加密字符串)来进行身份验证,而这个token又是根据TokenService自动生成的,要通过TokenService生成一个token,又需要一个合法的Microsoft Virtual Earth Platformdeveloper account……明白了这个过程,就来做我们的工作吧。
        首先, 去申请一个Microsoft Virtual Earth Platform developer account,当然之前你还得有一个Windows Live账号。申请的这个账号是Evaluation版的,所以决定了以后我们只能使用Staging的服务,如果要把它变成Production版本,可以通过邮件联系微软,然后缴费;
        之后到注册时所填的邮箱去激活申请的Microsoft Virtual Earth Platform developeraccount账号,然后为其设置密码(必须是8-14为之间,包括大、小写字母,数字,且还要有非字母数字的字符,和windows server2008是一样的),我们平常肯定不会这样设置密码,为了以防万一,建议赶紧把设置好的密码记录下来,
没准哪天就忘了。现在就可以用这个账户和密码来访问TokenService,通过它生成token,交给TileLayer的token属性。
        为了安全目的考虑,token是不建议也不能直接在Silverlight程序中进行设置的。那么怎么办呢?这样办:1、通过装载Silverlight的aspx页面的Page_Load方法,来申请我们的token,并把它添加到Silverlight的初始参数中,2、然后当Silverlight插件载入的时候,把token读出来,3、在Map_Loaded事件中,赋给TileLayer。
1、通过TokenService申请token:
在webapp中add webreference,url用 https://staging.common.virtualearth.net/find-30/common.asmx?wsdl,起个名字叫VirtualEarthService.TokenService。
  1. <script language="C#" runat="Server">
  2.     private string VEAccountID = "你的ID(注意只是AccountID)";
  3.     private string VEAccountPassword="你的密码";
  4.    
  5.     protected void Page_Load(object sender,EventArgs e)
  6.     {

  7. _08_virtual_earth.Web.VirtualEarthService.TokenService.CommonService
  8. commenservice = new
  9. _08_virtual_earth.Web.VirtualEarthService.TokenService.CommonService();
  10.         
  11.         commenservice.Credentials = new System.Net.NetworkCredential(VEAccountID, VEAccountPassword);


  12. _08_virtual_earth.Web.VirtualEarthService.TokenService.TokenSpecification
  13. tokenSpec=new
  14. _08_virtual_earth.Web.VirtualEarthService.TokenService.TokenSpecification();
  15.         tokenSpec.TokenValidityDurationMinutes=480;
  16.         if (HttpContext.Current!=null && !HttpContext.Current.Request.IsLocal)
  17.         {
  18.             tokenSpec.ClientIPAddress=HttpContext.Current.Request.UserHostAddress;
  19.         }
  20.         else
  21.         {
  22.             tokenSpec.ClientIPAddress="127.0.0.1";
  23.         }
  24.         
  25.         string token = "";
  26.         token = commenservice.GetClientToken(tokenSpec);

  27.         Xaml1.InitParameters = string.Format("token={0}", token);
  28.     }
  29. </script>
复制代码
其中Xaml1是Silverlight插件的ID:<asp:Silverlight ID="Xaml1" runat="server"...
2、Silverlight插件载入时读出这个token。在App.xaml.cs中:
  1. private void Application_Startup(object sender, StartupEventArgs e)
  2.         {
  3.             VEtoken = e.InitParams["token"];
  4.             this.RootVisual = new Page();
  5.         }
复制代码
3、最后在加载地图控件后,交付token:
  1. private void Map1_Loaded(object sender, RoutedEventArgs e)
  2.         {
  3.             foreach (Layer layer in Map1.Layers)
  4.                 if (layer is TileLayer)
  5.                     (layer as TileLayer).Token = (Application.Current as App).VEtoken;
  6.         }
复制代码
终于能看见VE的图了。当然,我们的开发账户是免费的,所以地图上有很多“Staging”麻点(每个tile一个):

08_ve.jpg


 



        至此,ArcGIS API for Silverlight的开发入门已经讲完了,我和大家一样也是边学边写的,刚好这两天SIlverlightAPI又升级了第二个Beta版。其实Silverlight和Flex一样,能使传统的WebGIS散发出全新的魅力,从而使我们的程序在RIA的道路上大踏步前进,能够做出什么样的效果也基本只受想象力的制约了。随着Silverlight3的推出,我们也有理由相信Silverlight的明天会更好。

原文地址:http://bbs.esrichina-bj.cn/ESRI/thread-45835-1-1.html




相关文章
|
API Windows
ArcGIS API for Silverlight动态标绘的实现
1、下载2个dll文件,分别是: ArcGISPlotSilverlightAPI.dll 和 Matrix.
738 0
|
API 定位技术 Windows
ArcGIS API for Silverlight 加载google地图
  using System; using System.Net; using System.
770 0
|
API 定位技术
ArcGis API FOR Silverlight 做了个导航工具~
原文 http://www.cnblogs.com/thinkaspx/archive/2012/08/08/2628214.html 转载请注明文章出处:http://www.cnblogs.com/thinkaspx 地图是读取的谷歌图层。
700 0