您的位置:首页 > 其它

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

2009-06-02 00:34 746 查看
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一个):





下载 (89.16 KB)

2009-5-6 18:16

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

原文地址:http://bbs.esrichina-bj.cn/ESRI/thread-45835-1-1.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐