Skip to content

Latest commit

 

History

History
58 lines (37 loc) · 4.34 KB

geocoding-using-silverlight.md

File metadata and controls

58 lines (37 loc) · 4.34 KB
title authors intro types categories published updated status
Geocoding using Silverlight
thebeebs
 [![geocode](images/8371.geocode_thumb_626FC0...
shorts
geocoding
silverlight-3
silverlight-4
mobile
2010/06/28 12:00:00
2010/06/28 13:00:00
archived

 geocode Last night I was reading through the Vision and Scope document for a project that we are involved in with the British Library, one of the requirements was to geocode a search term. If you are not familiar with the term geocoding then congratulations; you're getting out of the house enough. Geocoding is when you take a string of text like "Leaning Tower of Pisa" and convert it into Latitude and Longitude coordinates.

 

The British Library would like users who enter a search term into their Silverlight Application to be presented with Documents and Photographs that relate to the search term entered. If the search term is of geographical significance such as "The Baltic Sea" then relevant documents and photos, which have matching Latitude and longitude information in the metadata, should be displayed.

 

When I saw the requirement it made me think immediately of the an EasyJet accelerator that I had been working on the previous month. In that project I used the Bing web service to Geocode a search term so that we could figure out what the closest airports were to the search term entered. Geocoding using Bing is straight forward and will work on all most any platform as it only requires a post to a RESTful service and then to consume the JSON response.

 

The method below will work in Silverlight 3, Silverlight 4 and Windows Phone 7, I used a third party JSON library called JSON.net, this is because some JSON libraries are not present in the Windows Phone version of Silverlight and I thought as this would be a common requirement on the Phone I should make this sample compatible.

 

Firstly you will need to obtain an App Id which will give you permission to use the service. Go to https://www.bingmapsportal.com/ sign in and Create an App Id by clicking the "Create App ID" link and following the instructions.

 

By default the bing  service returns JSON, to consume JSON easily (especially in windows phone) we need to download http://json.codeplex.com/releases/view/43775 and add the Newtonsoft.Json.Silverlight.dll which you will find in the zip folder Json35r7\Bin\Silverlight.

 

In my sample application a user enters a search term into a text box and clicks a button. In my button click event logic I save the search term into a local variable and then using string.format I create a URL which is the RESTfull endpoint for the Bing webservice. Using a new Web Client object I make a request to the Bing service URL and delegate the result to the event handler client_DownloadStringCompleted.

The format of the URL is below you just need to replace <> and <> with your own:

http://dev.virtualearth.net/REST/v1/Locations?query=<>&key=<<appId>

 

clip_image001

Inside the method client_DownloadStringCompleted I create a new JObject to parse the returned string and then use the SelectToken method to take the pieces from the JSON that I require and save them to 2 local variables. I then string format these two values into a readable string and then send the result to the text property of a text block.

 

clip_image002

That's it... Happy Hacking.