For the MVPSummitEvents and Mix10Events site I wanted to create a map of all of the events listed on the site. In order to do that I needed to Geocode all of the addresses for the events. There are several services out there for geocoding an address, Microsoft, Yahoo, and Google provide this service as well as others. I decided to go with the Microsoft Bing services, being a Microsoft MVP.
Let’s get started. MSDN has just about everything you need to get started with using the Bing Map Web Services.
Step 1: The first step is to get a key or token to use in your application for the Bing Maps Web Services application. This can be done by visiting the Bing Maps Account center and clicking on Create a Bing Maps account.
Step 2: If you are using Visual Studio, add a service reference to one or more Bing Maps Web Services that provide the features you need. See the Generating Client Proxy Classes topic and the Bing Maps Web Services Metadata topic.
Whether you used Visual Studio or the svcutil application you should have one file, most likely named VirtualEarthWebServices.cs. The file will contain a bunch of wrapper classes around the Bing Maps Web Services, and the required Windows Communication Foundation (WCF) classes. You will also see the generated configuration settings for the app or web config files.
Step 3: Set every Bing Maps Web Services request a valid Credentials property. You will see more on this in a bit.
Geocoding an Address
There are two properties that are required to successfully request a GeoCode for an address.
- 1) Set the Credential Property of the GeoCodeRequest object
- 2) Set either the Query property or Address property of the GeoCodeRequest object.
Here is a helper function that wraps the call to GeoCodeRequest.
This method will return a GeocodeResponse object. The GeocodeResponse object contains three properties that are populated based on the query.
|BrandLogoUri||The System.Uri of the Bing Maps brand logo image. (Inherited from the ResponseBase Class.)|
|ResponseSummary||A ResponseSummary Class object describing the response that was returned by the service. (Inherited from the ResponseBase Class.) This class returns any exceptions that we raised during the request.|
|Results||A GeocodeResult Class array, where each element is a possible match returned by the Geocode Service.|
To keep this article short(er) I will just cover the Results object. Depending on the Confidence filter and Geocode options that were set in the call you could receive more than one result.
Let’s assume that we only want to work with the first result and get the Geocode for “1 Microsoft Way, Redmond, WA”. We simply call the static method of GetGeocodeResponse and pass in the Bing Maps API key and the address to search for.
Assuming the address was found we can now work with the properties of the GeocodeResult class to find out the Geocode. The Geocode is located in the Locations property which is an array of GeocodeLocation objects. If the Count of the Locations is greater than one, let’s just take the first one and update the txtLatitude and txtLongitude objects.
That’s it. Next up, using the Bing Maps Web Services for getting map images.