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

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

概述SilverlightAPI中还包括了一个ESRI.ArcGIS.VirtualEarth.dll类库,让我们可以方便的访问到老东家的VirtualEarth服务。目前SilverlightAPI中提供的VirtualEarth服务有三种:Map,Geocode和Routing,不过一看就知道后两种服务对于国内的数据来说又无缘了。         直接看如何使用它的Map服务获取地图数据吧。同前, SilverlightAPI中还包括了一个ESRI.ArcGIS.VirtualEarth.dll类库,让我们可以方便的访问到老东家的VirtualEarth服务。目前SilverlightAPI中提供的VirtualEarth服务有三种:Map,Geocode和Routing,不过一看就知道后两种服务对于国内的数据来说又无缘了。
        直接看如何使用它的Map服务获取地图数据吧。同前,新建一个Silverlight工程,添加ESRI.ArcGIS.dll和ESRI.ArcGIS.VirtualEarth.dll的引用,引入xml命名空间,在xaml里面这样写: @H_404_13@<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。 @H_404_13@<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中: @H_404_13@private voID Application_Startup(object sender,StartupEventArgs e)
        {
            Vetoken = e.InitParams["token"];
            this.RootVisual = new Page();
        } 复制代码 3、最后在加载地图控件后,交付token: @H_404_13@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的道路上大踏步前进,能够做出什么样的效果也基本只受想象力的制约了。

总结

以上是内存溢出为你收集整理的ArcGIS API for Silverlight开发入门(8):在程序中使用Virtual Earth的服务全部内容,希望文章能够帮你解决ArcGIS API for Silverlight开发入门(8):在程序中使用Virtual Earth的服务所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1065591.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存