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;
}
}
}