ArcGIS API for Silverlight開發入門(8):在程式中使用Virtual Earth的服務

來源:互聯網
上載者:User
  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一個):

下載 (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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.