C# Microsoft MapPoint 4.5 Geocode (Latitude and Longitude) Class

by Donn Felker 7. May 2007 04:18

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 Microsoft MapPoint SDK

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)


kick it on DotNetKicks.com

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.NET | GIS

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

Donn Felker

Senior Consultant
MCTS
ScrumMaster
Agile Practitioner

About Me | Books I Recommend

Gotta Pay The Bills


Tag cloud

    Popular Posts

    RecentComments

    Comment RSS

    Calendar

    <<  January 2009  >>
    MoTuWeThFrSaSu
    2930311234
    567891011
    12131415161718
    19202122232425
    2627282930311
    2345678

    View posts in large calendar