SilverlightAPI中还包括了一个ESRI.ArcGIS.VirtualEarth.dll类库,让我们可以方便的访问到老东家的VirtualEarth服务。目前SilverlightAPI中提供的VirtualEarth服务有三种:Map,Geocode和Routing,不过一看就知道后两种服务对于国内的数据来说又无缘了。
直接看如何使用它的Map服务获取地图数据吧。同前,新建一个Silverlight工程,添加ESRI.ArcGIS.dll和ESRI.ArcGIS.VirtualEarth.dll的引用,引入xml命名空间,在xaml里面这样写:
<esri:Map x:Name="Map1" Loaded="Map1_Loaded">
<esri:Map.Layers>
<esriVE:TileLayer ID="VELayer" LayerStyle="AerialWithLabels" ServerType="Staging"/>
</esri:Map.Layers>
</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。
<script language="C#" runat="Server">
private string VEAccountID = "你的ID(注意只是AccountID)";
private string VEAccountPassword="你的密码";
protected void Page_Load(object sender,EventArgs e)
{
_08_virtual_earth.Web.VirtualEarthService.TokenService.CommonService
commenservice = new
_08_virtual_earth.Web.VirtualEarthService.TokenService.CommonService();
commenservice.Credentials = new System.Net.NetworkCredential(VEAccountID, VEAccountPassword);
_08_virtual_earth.Web.VirtualEarthService.TokenService.TokenSpecification
tokenSpec=new
_08_virtual_earth.Web.VirtualEarthService.TokenService.TokenSpecification();
tokenSpec.TokenValidityDurationMinutes=480;
if (HttpContext.Current!=null && !HttpContext.Current.Request.IsLocal)
{
tokenSpec.ClientIPAddress=HttpContext.Current.Request.UserHostAddress;
}
else
{
tokenSpec.ClientIPAddress="127.0.0.1";
}
string token = "";
token = commenservice.GetClientToken(tokenSpec);
Xaml1.InitParameters = string.Format("token={0}", token);
}
</script>
复制代码
其中Xaml1是Silverlight插件的ID:<asp:Silverlight ID="Xaml1" runat="server"...
2、Silverlight插件载入时读出这个token。在App.xaml.cs中:
private void Application_Startup(object sender, StartupEventArgs e)
{
VEtoken = e.InitParams["token"];
this.RootVisual = new Page();
}
复制代码
3、最后在加载地图控件后,交付token:
private void Map1_Loaded(object sender, RoutedEventArgs e)
{
foreach (Layer layer in Map1.Layers)
if (layer is TileLayer)
(layer as TileLayer).Token = (Application.Current as App).VEtoken;
}
复制代码
终于能看见VE的图了。当然,我们的开发账户是免费的,所以地图上有很多“Staging”麻点(每个tile一个):
至此,ArcGIS API for Silverlight的开发入门已经讲完了,我和大家一样也是边学边写的,刚好这两天SIlverlightAPI又升级了第二个Beta版。其实Silverlight和Flex一样,能使传统的WebGIS散发出全新的魅力,从而使我们的程序在RIA的道路上大踏步前进,能够做出什么样的效果也基本只受想象力的制约了。随着Silverlight3的推出,我们也有理由相信Silverlight的明天会更好。
分享到:
相关推荐
- **使用Virtual Earth服务**:通过集成ESRI.ArcGIS.Client.Bing.dll类库,可以在应用中利用Virtual Earth提供的地图服务。 #### 六、总结 通过使用ArcGIS API for Silverlight,开发者可以轻松地构建出具有丰富...
### ArcGIS RIA 开发讲座 for Silverlight API #### 一、Silverlight 技术概述 **Silverlight** 是微软推出的一种跨浏览器的富互联网应用程序(RIA)框架,旨在为 Web 开发者提供一个强大的平台来创建具有高度交互...
- **定义**:ArcGIS Silverlight API 是一款基于 Silverlight 的 WebGIS 应用开发接口,它通过 REST 接口访问 ArcGIS Server 服务,并可以直接访问微软 Bing 地图服务。 ##### 版本历史 - **2009 年 3 月**:发布 ...
例如,ArcGIS JavaScript Extension for Virtual Earth API 和 ArcGIS JavaScript Extension for Google Maps API 就是两个常用的扩展,它们可以帮助开发者将ArcGIS Server的地图服务集成到第三方地图平台中。...
ArcGIS API for Silverlight的推出,使得开发者能够在.NET环境下创建更加丰富的ArcGIS Server Web应用程序。这一API集成了大量高级功能,例如地图操作、空间查询等,为用户提供了一个灵活且强大的开发框架。 ##### ...
ArcGIS API for Silverlight的引入是9.3.1版本中的另一亮点,它为开发者提供了强大的工具集,用于创建基于ArcGIS Server的Web应用程序。这不仅拓展了ArcGIS的应用领域,还为用户界面设计和用户体验带来新的可能。...