Just a couple of days ago, the Bing Maps team broke out the new production-ready release of the “Bing Maps Silverlight Control” which replaces the “Virtual Earth Silverlight Map Control CTP” that had been first revealed back at MIX09 in March. If you’ve played about with the CTP, you’ll find that much of your application code will need only a bit of minor modification (eg: namespaces, events and some custom binding routines) but the biggest mandatory change is a operational one – licensing. If you are starting afresh it will affect you too…

Bing Maps traditionally counted how many tiles and service calls an application made to the Microsoft servers and charged appropriately – but Bing Maps have removed this barrier and moved to a per-session giving both developers and not-for-profit organisations a much easier process for using maps without costing the earth (pun intended!). For example, developers using the Bing Maps Platform get 125,000 mapping sessions per year for FREE and get 500,000 Bing Maps Web Service transactions a year for FREE. For all the relevant details see here, which provides a proper summary and links to the full terms and conditions. It should be noted that the Bing Maps Platform consists of both a Silverlight 3 control, AJAX control and lookup Web Services (for geo-locating, directory, routing) and the definitions vary somewhat to cover the variations of usage and what imagery is available – for instance Birds Eye View is not currently available and businesses need to buy licences etc..

Anyway, the important part is that you are going to need an application key which is which is linked to you and your hosting domain. The key is embedded within your application so that the Bing Maps Silverlight Control can extract it and pass it through to the map tile server and various web service endpoints to validate your account and quota. Under the developer rules, you’ll only be able to generate 5 keys… make a mistake and you are foobar’ed because the account management is so basic, there is only add and no remove!

Open up, the Bing Maps Platform Account Management Center and you’ll be asked to sign-in with a valid Windows Live ID. Note, this account will be the only account that will be able to manage the application key, so choose wisely or use a team account if you are a band of developers. Also, pay particular to the EULA / terms & conditions for using the Bing Maps Services.

 Bing Maps Account Center

Once signed in, the key management screen is displayed. On a developer/free account, you can only generate 5 keys – and you cannot correct any mistakes, so type carefully (I’ve learnt from bitter experience already!). The application name can be any short name or namespace that you use to identify your project (eg: I used “BingMapsBlogPost” in the example below). The application URL is the name of the domain that the Silverlight/AJAX application will be run from – this is important – if you are going to host from “www.somewebhostname.com” then that is what you use… check your spelling twice over before submitting. If all is successful, you’ll get a record displaying your application name, URL and key.

 Create Bing Map Keys

The application key will be embedded within your web application and the Bing Maps Services will check the Referrer URL and the application key before servicing the request. That is why entering the correct application URL is very important.

Next, lets get stuck into the code. Of course, this is a introduction to the Bing Maps Silverlight Control so you will need to have Visual Studio 2008 with the Silverlight 3 runtime and SDK installed. Download and install the Bing Maps Silverlight Control afterwards.

Now, open up Visual Studio and create a basic Silverlight project, File | New Project

 Create New Project

I normally create a hosting website for the Silverlight application by accepting the defaults on the next popup.

  New Silverlight Application

This results in a project structure such as that shown below.

 Solution Explorer

In order for the application to use the Bing Maps Silverlight Control, we’ll need to add a reference to it in the Silverlight application project. As a matter of my own practice, I usually copy any referenced 3rd party assemblies into a folder relative to the project, thus the assemblies are in the following folder (or similar if you are running 32bit Windows).

C:\Program Files (x86)\Bing Maps Silverlight Control\V1\Libraries

 Referenced Assemblies

  Add Reference

Once the references have been added, the map control can be declared, and configured with the application key setup previously. I’ve highlighted the crucial bits of XAML that makes the map display… the other elements within the <m:Map> tag just settle the map to my London office and stretch it to encompass the entire browser window. The CredentialsProvider attribute has a default overload for just specifying the Application Key however there many more options available, but I’ve opted for the simplest case.

  MainPage.xaml

Alternatively, if you don’t want to set the application key in the XAML (for instance you might need to use the key for the service calls to the other Bing Map Platform functions) then you can create a valid CredentialsProvider by hand. One of the best places to populate it is when the map element has loaded (again, you can hook up the event to pass deeper into your business logic but I’m just wiring it up in the MainPage.xaml ’s code-behind) so create an event handler from the map control’s Loaded event, and add the details into an ApplicationIdCredentialsProvider object which can be passed into the CredentialsProvider. It may be a little extra work, but this way you can share the key about the application instead of embedding it within the XAML markup or a XAML StringResource.

 MainPage.xaml.cs

Deploying and running the application will spin up the Bing Map in your browser as below. Remember that the application key is directly linked to the domain that the browser is pointing at otherwise you’ll get a neat error in the centre of the map telling you that the application key is invalid.

  Bing Maps View

That’s it! It’s really that straightforward to get going with just a little bit of forward planning, but this is really the tip of the iceberg, the Bing Maps Platform is far more reaching than only displaying simple maps.

For more information, check out the links below and exploit the Bing Maps Platform whether it be Silverlight or AJAX based.

UK, Silverlight, Microsoft, Bing, Bing Maps, StreetSide, Birds Eye, StreetView, EMC, EMC Consulting

 

This is a cross post from my EMC blog, mainly for backup duplicity and to aggregate some of my past postings. My EMC blog used to be under the Conchango brand but was acquired by EMC so I’ve also retrospectively refreshed some of the old links and maybe a tweak a bit of content too.
permalink to the original post here