EziData Solutions

Web, NET and SQL Server

Using the Bing Maps API

As part of developing a community directory website I needed to migrate a large number of Name/Address records from several existing systems. These records contained the usual street and suburb fields, but lacked Latitude and Longitude values.

Instead of doing the geocoding when the record is displayed in the map, I decided to utilise the Bing Maps API to perform the geooding of these addresses and store the resulting Latitude and Longitude in the database.

To perform the geocoding I used the Geocode Service, which is part of Bing Maps SOAP Services. According to their website

Microsoft® Bing Maps SOAP Services is a set of programmable SOAP services that allow you to match addresses to the map, search for points of interest, integrate maps and imagery, return driving directions, and incorporate other location intelligence into your Web application.

The first step is to sign up for your Bing Maps API key – it's free and painless, especially if you have a Windows Live account already.

Our Address Class

As this project was dealing with the names and addresses of organisations, I created a simple class to store the Organisation details and another to store the Addresses where the organisation operated. The important parts of the OrganisationAddress class are shown below.

public class OrganisationAddress

{

    public string StreetAddress { get; set; }

    public double Latitude { get; set; }

    public double Longitude { get; set; }

}

As each new OrganisationAddress was created, I called a function that passed the address to the Geocode Service and returned the Lat and Long, which I then saved with the OrganisationAddress.

 

To get started, add a Service Reference named MyBingMap to your Visual Studio project. The Uri to the GeocodeService is http://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc?wsdl

Once you have the service reference, you can create a function that send the street address (consisting of the house number, street name and suburb) and as a GeocodeRequest object to the Bing Maps API. The snipet below shows part of the function that sends in our OrganisationAddress object that contains that StreetAddress property.

    //instantiate a GeocodeRequest and pass in your Bing Maps API key

    MyBingMap.GeocodeRequest map = new MyBingMap.GeocodeRequest();

    map.Credentials = new MyBingMap.Credentials();

    map.Credentials.ApplicationId = "Your API Key";

 

    //add the address components to the GeocodeRequest.Query

    map.Query = OrganisationAddress.StreetAddress;

 

    //the Confidence level controls how accurate you want the results

    //if your address is good then Confidence.High should return the location

    //but be warned, if the location can not be found you'll get lat,long of 0,0

    MyBingMap.ConfidenceFilter[] filters = new MyBingMap.ConfidenceFilter[1];

    filters[0] = new MyBingMap.ConfidenceFilter();

    filters[0].MinimumConfidence = MyBingMap.Confidence.High;

 

    //add the assigned ConfidenceFilter to the Options

    MyBingMap.GeocodeOptions options = new MyBingMap.GeocodeOptions();

    options.Filters = filters;

    map.Options = options;

 

    //instantiate a GeocodeResponse for the service passing in your GeocodeRequest

    MyBingMap.GeocodeServiceClient geoClient = new MyBingMap.GeocodeServiceClient("BasicHttpBinding_IGeocodeService");

    MyBingMap.GeocodeResponse geocodeResponse = geoClient.Geocode(map);

 

    //if we get a response back, then save the Lat/Long with our OrganisationAddress

    if (geocodeResponse.Results.Length > 0)

    {

        if (geocodeResponse.Results[0].Locations.Length > 0)

        {

            OrganisationAddress.Latitude = geocodeResponse.Results[0].Locations[0].Latitude;

            OrganisationAddress.Longitude = geocodeResponse.Results[0].Locations[0].Longitude;

        }

    }

If like me you get an error "The remote server returned an unexpected response: (407) Proxy Authentication Required" chances are it's because you are using a proxy server to access the web. In this case you'll need to add a few extra lines of code at the start of the previous code block to setup the credentials of the DefaultWebProxy.

    //if you're accessing the service via a proxy

    //you may need to specify your credentials

    IWebProxy wproxy = WebRequest.DefaultWebProxy;

 

    wproxy.Credentials = CredentialCache.DefaultNetworkCredentials;

    WebRequest.DefaultWebProxy = wproxy;

Posted: Apr 13 2011, 05:16 by CameronM | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: ASP.NET