Can you believe it? We’re at the end of this (longer than it should have been) journey. Today is the last day of the 31 Days of Silverlight. We are going to be building a more advanced version of the application we started yesterday, Day #30, related to Bing Maps in Silverlight.
1. Geocoding an address
For those of you relatively new to mapping, there’s this wonderful concept called Geocoding which makes our lives as developers much, much simpler. The basic premise is that we can provide our application with an actual address, and have a geocoding service figure out the latitude/longitude coordinates of that address. This way, we don’t have to tell our users to figure it out for themselves. Can you imagine going to Bing Maps to look at your house, only to be told you need to know the lat/long of that location? Not exactly user friendly. So, let’s get to it.
2. Adding the Geocoding service to our application
Before we can geocode an address, we need to add a reference to Bing’s geocoding web service. So let’s start there. Click on your Silverlight project, and choose “Add service reference…” Use this address as your address, and click Go:
You should end up with a window that looks like this:
3. Creating an asynchronous call to that service
Much like talking to any web service, we have a couple of things we need to do. The first is to create a new instance of the service. The second is to attach a Completed event handler to the service, so that we know when a response has occurred. Finally, we need to create our service request, and call the service asynchronously. In my example, I created a method called Geocode that takes the same two parameters as the web service: an address, and a waypointIndex. I’ll explain waypointIndex later, when we talk about routing and directions. So, here’s what my method looks like:
private void Geocode(string address, int waypointIndex)
GeocodingService.GeocodeServiceClient geocodingService = new GeocodingService.GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
geocodingService.GeocodeCompleted += new EventHandler<SilverlightBingMapsGeocoding.GeocodingService.GeocodeCompletedEventArgs>(geocodingService_GeocodeCompleted);
GeocodingService.GeocodeRequest request = new GeocodingService.GeocodeRequest();
request.Credentials = new Credentials();
request.Credentials.ApplicationId = ((ApplicationIdCredentialsProvider)myMap.CredentialsProvider).ApplicationId;
request.Query = address;
You can see that we also have to attach our API Credentials that we got in Day #30 to our request. Without a valid API key, we can’t make requests. Since I stored mine in the XAML, the reference you see in my code above is to that XAML location. For the Completed event handler, we need the event method for that as well. In mine, I am capturing the new Location object it returns, and adding that as a pushpin to my map, like we did yesterday. This solution only works when you’re certain the service will only be returning one value to you. Here’s the code:
void geocodingService_GeocodeCompleted(object sender, SilverlightBingMapsGeocoding.GeocodingService.GeocodeCompletedEventArgs e)
Location center = e.Result.Results.Locations;
myMap.Center = center;
Pushpin pushpin = new Pushpin();
pushpin.Location = center;
After all of that is done, we just need to call our Geocode() method. In my example, I’m using the address of my office again. Here’s the method call:
Geocode("8800 Lyra Avenue, Columbus, OH 43240", 1);
4. Geocoding’s easy. How about turn-by-turn?
Directions are a little trickier, but not significantly. In fact, we wouldn’t need to change our Geocode method one bit. The major changes we make to our application include using another web service, the RouteService, and adding the Waypoints that it returns to your map.
There’s an excellent explanation of this on the MSDN reference for Bing Maps in Silverlight. You can read it here. http://msdn.microsoft.com/en-us/library/ee681887.aspx
I was originally planning on writing out an entire tutorial for this section as well, but after reading their explanation, I realized I’d only be taking away from an excellent article.
Getting an address to a latitude and longitude point is nothing to sneeze at. I don’t even want to think about what is required to happen inside those services. I’m happy to have this nice, thick layer of abstraction to manipulate. I think you’ll find as you go forward that the route plotting is really just as simple.
This series on Silverlight has certainly been quite an adventure. I hope that you had an opportunity to learn a few things along the way. I certainly know that I did.
This coming February 2010, I am going to be starting another series, with a slightly less focused approach. They will all be related to software development and technology, but I’m going to branch out a bit. My first two posts will cover creating a theme in Windows 7, and the second will cover XNA development for the Zune. I also have plans to cover topics like ASP.NET MVC 2, among others. If there’s something you’d like to see me cover in this as-to-be-named series, please let me know, and I’ll do my best to accomodate you. For now, however, I’m signing off of this 31 Days of Silverlight, and look forward to the next adventure.
P.S. Tune in tomorrow for a game I created in Silverlight. The first 5 people to finish it will win a free O’Reilly ebook of their choice!