ArcGIS API for Silverlight development (8): Using Virtual Earth services in a program

Source: Internet
Author: User
Tags alphanumeric characters
SilverlightAPI also includes an ESRI. ArcGIS. VirtualEarth. dll class library, which allows us to conveniently access the VirtualEarth service of the old house. Currently, SilverlightAPI provides three VirtualEarth services: Map, Geocode, and Routing. However, at first glance, we can see that the last two services are unavailable for domestic data.
Let's look at how to use its Map service to obtain Map data. Create a Silverlight project, add references to ESRI. ArcGIS. dll and ESRI. ArcGIS. VirtualEarth. dll, and introduce the xml namespace. Write in The xaml as follows:

  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>

Copy code

It can be seen that it is basically the same as adding other layers. In SIlverlightAPI, the layer types of VE maps are TileLayer, and LayerStyle has three types: Road, Aerial, and AerialWithLabels, which correspond to vector, image, and street-marked image respectively. ServerType is special. There are two types: Staging and Production, which correspond to the account categories for accessing the VE service. The former is free, and the latter is charged. If you run the program at this time, you cannot see the map, because TileLayer has a key token attribute that is not set.
The VE service is quite secure. Every time you access the VE service, you must provide a token (an encrypted string) for identity authentication. This token is automatically generated based on the TokenService, to generate a token through TokenService, you need a valid Microsoft Virtual Earth Platformdeveloper account ...... If you understand this process, let's do our work.
First, apply for a Microsoft Virtual Earth Platform developer account. Of course, you have to have a Windows Live account before. This account is applied for in the Evaluation version, so we can only use the Staging service in the future. If you want to change it to the Production version, you can contact Microsoft via email and pay the fee;
Then, activate the applied Microsoft Virtual Earth Platform Guest Account in the registered email address, and set the password for the Account (which must be between 8 and 14, including uppercase and lowercase letters and numbers, it must contain non-alphanumeric characters, which is the same as that of windows Server). We certainly do not set a password like this. To prevent this, we recommend that you record the password,
Maybe I forgot one day. Now you can use this account and password to access TokenService, generate token through it, and hand it over to the token attribute of TileLayer.
For security purposes, token is not recommended and cannot be set directly in the Silverlight program. So what should we do? 1. Apply for our token by loading the Page_Load method of the aspx page of Silverlight and add it to the initial parameters of Silverlight, 2. When the Silverlight plug-in is loaded, read the token. 3. assign it to TileLayer in the Map_Loaded event.
1. Apply for a token through TokenService:
Add webreference in webapp, url with https://staging.common.virtualearth.net/find-30/common.asmx? The name is VirtualEarthService. TokenService.

  1. <Script language = "C #" runat = "Server">
  2. Private string VEAccountID = "your ID (note only AccountID )";
  3. Private string VEAccountPassword = "your password ";
  4. Protected void Page_Load (object sender, EventArgs e)
  5. {
  6. _ 08_virtual_earth.Web.VirtualEarthService.TokenService.CommonService
  7. Commenservice = new
  8. _ 08_virtual_earth.Web.VirtualEarthService.TokenService.CommonService ();
  9. Commenservice. Credentials = new System. Net. NetworkCredential (VEAccountID, VEAccountPassword );
  10. _ 08_virtual_earth.Web.VirtualEarthService.TokenService.TokenSpecification
  11. TokenSpec = new
  12. _ 08_virtual_earth.Web.VirtualEarthService.TokenService.TokenSpecification ();
  13. TokenSpec. TokenValidityDurationMinutes = 480;
  14. If (HttpContext. Current! = Null &&! HttpContext. Current. Request. IsLocal)
  15. {
  16. TokenSpec. ClientIPAddress = HttpContext. Current. Request. UserHostAddress;
  17. }
  18. Else
  19. {
  20. TokenSpec. ClientIPAddress = "127.0.0.1 ";
  21. }
  22. String token = "";
  23. Token = commenservice. GetClientToken (tokenSpec );
  24. Xaml1.InitParameters = string. Format ("token = {0}", token );
  25. }
  26. </Script>

Copy code

Xaml1 is the Silverlight plug-in ID: <asp: Silverlight ID = "Xaml1" runat = "server "...
2. Read the token when the Silverlight plug-in is loaded. In App. xaml. cs:

  1. Private void Application_Startup (object sender, StartupEventArgs e)
  2. {
  3. VEtoken = e. InitParams ["token"];
  4. This. RootVisual = new Page ();
  5. }

Copy code

3. Finally, after loading the map control, deliver the 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. }

Copy code

Finally, we can see the VE diagram. Of course, our development account is free of charge, so there are many "Staging" points on the map (one for each tile ):

Download(89.16 KB)

 

So far, the development of ArcGIS API for Silverlight has been completed, and I have learned and written it like everyone else. Just two days ago, SIlverlightAPI upgraded the second Beta version. In fact, like Flex, Silverlight can bring new charm to the traditional WebGIS, so that our programs can stride forward on the road of RIA, what effects can be achieved is basically restricted by imagination. With the introduction of Silverlight3, we also have reason to believe that the future of Silverlight will be better.

Address: http://bbs.esrichina-bj.cn/ESRI/thread-45835-1-1.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.