Geocoding / Address Validation with MSN Virtual Earth

By Peter Bromberg
Access over 40 UI widgets with everything from interactive menus to rich charts.

The following class provides everything needed to submit an address to the Virtual Earth engine and return a corrected address with latitude and longitude coordinates:

using System;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
namespace VE
{
public class Search
{
public static object[] SearchAddress(string address)
{
object[] foundResults = Search.SearchForAddress(address);
if (foundResults.Length==3)
{
return foundResults;
}
else
{
return null;
}
}

public static string ProxyAddress
{
get { return proxyAddress; }
set { proxyAddress = value; }
}

static string proxyAddress =String.Empty;

public static string ProxyUserName
{
get { return proxyUserName; }
set { proxyUserName = value; }
}

static string proxyUserName = String.Empty;

public static string ProxyPassword
{
get { return proxyPassword; }
set { proxyPassword = value; }
}

static string proxyPassword = String.Empty;
public static object[] SearchForAddress(
string address)
{
double pointLat=0;
double pointLong=0;
string pointAddr=String.Empty;
string searchParams =
"a=&b=" + address + "&c=0.0&d=0.0&e=0.0&f=0.0&g=&i=&r=0";
string results = DoSearchRequest(searchParams);
if (results == null || results == string.Empty)
{
return null;
}
// this gets the first set of javascript returned, for viewport, which we don't need
// Parse results
// Regex r = new Regex(@"SetViewport\((?<lat1>\S+),(?<long1>\S+),(?<lat2>\S+),(?<long2>\S+)\)");
// Match m = r.Match(results);
try
{
Regex r2 = new Regex(@"VE_Scratchpad.AddLocation\((?<pointAddr>.+\S+), (?<pointLat>\S+), (?<pointLong>\S+)\)") ;
Match m2 = r2.Match(results);
pointAddr=(string)m2.Groups["pointAddr"].Value;
pointAddr= pointAddr.Replace("'","") ;
pointLat=double.Parse(m2.Groups["pointLat"].Value) ;
pointLong = double.Parse(m2.Groups["pointLong"].Value );
}
catch
{}
object[] res= new object[] {pointAddr,pointLat,pointLong};
return res;
}

private static string DoSearchRequest(string searchParams)
{
string results = String.Empty;
HttpWebRequest searchRequest =
(HttpWebRequest)WebRequest.Create( "http://207.46.159.133/search.ashx" );
searchRequest.Method = "POST";
if(Search.proxyAddress.Length >0)
{
WebProxy loProxy = new WebProxy( Search.proxyAddress ,true);
loProxy.Credentials = new NetworkCredential(Search.proxyUserName ,Search.proxyPassword);
searchRequest.Proxy = loProxy;
}
searchRequest.ContentType = "application/x-www-form-urlencoded";
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] bytes = encoding.GetBytes(searchParams);
searchRequest.ContentLength = bytes.Length;
try
{
Stream requestStream = searchRequest.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
results = GetSearchResults(searchRequest);
}
catch (WebException ex)
{
System.Diagnostics.Debug.Write(ex.Message +ex.StackTrace ) ;
}
return results;
}

private static string GetSearchResults(HttpWebRequest searchRequest)
{
string results = string.Empty;
HttpWebResponse searchResponse;
searchResponse =
(HttpWebResponse)searchRequest.GetResponse();
Stream receiveStream = searchResponse.GetResponseStream();
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(receiveStream, encode);
Char[] read = new Char[256];
int count = readStream.Read(read, 0, 256);
while (count > 0)
{
String str = new String(read, 0, count);
results += str;
count = readStream.Read(read, 0, 256);
}
searchResponse.Close();
return results;
}
}
}


Submission Date:  10/23/2005 3:56:57 PM
Submitted By:  Peter Bromberg
My Home Page:  http://www.eggheadcafe.com

Popularity  (234 Views)