A while back I created a Geocode Class that retrieved its Geocode information from Google Maps and its been downloaded a ton. I’ve also received a few emails asking if I had a Microsoft MapPoint implementation of the Geocode address. Apparently the examples in Microsofts SDK are not as easily accessible compared to that of Googles (from what others have said). So here it is, a Geocode class for Microsoft MapPoint.
I actually saved this as a project because you need the web reference and also a few settings in the app.config. You can download it, change a configuration setting, update a web reference, and then build and go. Please note, to test this, you’ll need NUnit or something similar. I have included a test class that has one unit test in it. This is so you can be sure that the code is working correctly. The actual Geocode class is in the project.
The SDK is full featured with a TON of classes to help with Geospatial programming. Check out the SDK when you get a chance.
Steps To get it up and running
1. Download the project at the bottom of this post
2. Get a developer account here.
3. Put the User/Pass word in the app.config file. It should be on line 10 of the app.config.
4. Update your web reference (right click on the web reference and click “Update Web Reference”).
5. Run the unit test. I use TestDriven.NET for this.
Options
The code has a class called FindOptions which allows you to set the threshold of how close you want the search result to be. Look at the MapPointGeocode.cs file below for more info.
Code
The GeoCodeClass (please note, for this to work, you’ll need the web reference to the MapPoint webservice. Download the example at the bottom of the post which has all references included.)
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Configuration;
using MapPointGeocode.MapPointService;
using System.Web.Services.Protocols;
using System.Diagnostics;
namespace MapPointGeocode
{
public class MapPointGeocode
{
/// <summary>
/// These are the actual instances of the objects that call the MapPoint .NET service
/// </summary>
private MapPointService.RenderServiceSoap renderService;
private MapPointService.FindServiceSoap findService;
public MapPointGeocode()
{
InstantiateServices();
}
private void InstantiateServices()
{
// Create and set the logon information (note comment in web.config — here would be the place to
// decrypt/unhash the user/password from the config file).
//NEW – Revised configuration settings (add ref to System.Configuration first):
NetworkCredential ourCredentials = new NetworkCredential(ConfigurationManager.AppSettings[“MPUser”], ConfigurationManager.AppSettings[“MPPass”]);
// Create the render service, pointing at the correct location
renderService = new MapPointService.RenderServiceSoap();
renderService.Credentials = ourCredentials;
renderService.PreAuthenticate = true;
// Create the find service, pointing at the correct location
findService = new MapPointService.FindServiceSoap();
// set the logon information
findService.Credentials = ourCredentials;
findService.PreAuthenticate = true;
}
/// <summary>
/// Returns the geocode coordinates of an address.
/// </summary>
/// <param name=”addressLine”>The address</param>
/// <param name=”city”>The city</param>
/// <param name=”postalCode”>The postal/zip code</param>
/// <param name=”country”>The country. e.g.: USA, Canada</param>
public LatLong GeocodeAddress(string addressLine, string city, string state, string postalCode, string country)
{
// Set up the address
Address address = new Address();
address.AddressLine = addressLine;
address.PrimaryCity = city;
address.PostalCode = postalCode;
address.Subdivision = state;
address.CountryRegion = country;
// Set up the specification for the address
// Set up the specification object.
FindAddressSpecification findAddressSpec = new FindAddressSpecification();
findAddressSpec.InputAddress = address;
findAddressSpec.DataSourceName = “MapPoint.NA”; // More info: http://msdn2.microsoft.com/en-us/library/ms982198.aspx and http://msdn2.microsoft.com/en-us/library/aa493004.aspx
// Set the find options. Allow more return values by decreasing
// the value of the ThresholdScore option.
// Also, limit the number of results returned to 20.
FindOptions myFindOptions = new FindOptions();
myFindOptions.ThresholdScore = 0.5;
myFindOptions.Range = new FindRange();
myFindOptions.Range.StartIndex = 0;
myFindOptions.Range.Count = 20;
findAddressSpec.Options = myFindOptions;
// Create a FindResults object to store the results of the FindAddress request.
FindResults myFindResults;
LatLong latLong = new LatLong();
try
{
// Get the results and return them if there are any.
myFindResults = findService.FindAddress(findAddressSpec);
FindResult[] myResults = myFindResults.Results;
if(myResults!= null)
{
latLong = myResults[0].FoundLocation.LatLong;
}
}
catch (SoapException myException)
{
// Your exception handling process goes here.
Debug.Write(myException);
}
return latLong;
}
}
}
Conclusion
The Microsoft MapPoint SDK might be a little more difficult to grok at first but one you start working with it you can see that it is full featured with many more options than the Google provided services.
Microsoft MapPoint 4.5 SDK Online Link
Download
MapPointGeocode.zip (276.41 KB)
Leave a Reply
You must be logged in to post a comment.