using System; using System.Web; using System.Net; using System.IO; using System.Collections; namespace MyVirtualEarth.V2 { public class RoutePoint { public string instruction; public decimal lat; public decimal lon; } public class Directions : System.Web.UI.Page { private string startLat; private string startLon; private string endLat; private string endLon; private ArrayList routePoints; private void Page_Load(object sender, System.EventArgs e) { if(Request["startLon"] != null && Request["startLat"] != null && Request["endLon"] != null && Request["endLat"] != null ) { Response.Clear(); startLat = Request["startLat"]; startLon = Request["startLon"]; endLat = Request["endLat"]; endLon = Request["endLon"]; string directions = GetDirections(); routePoints = GetRoutePoints(directions); foreach(RoutePoint routePt in routePoints) { Response.Write(routePt.instruction + "
"); Response.Write("Lat: "+ routePt.lat.ToString() +"
"); Response.Write("Lon: "+ routePt.lon.ToString() +"
"); } } } private ArrayList GetRoutePoints(string directions) { routePoints = new ArrayList(); // look for new VE_RouteInstruction( string routePtStart = "new VE_RouteInstruction('"; int currentIndex = 0; do { currentIndex = directions.IndexOf(routePtStart, currentIndex); if (currentIndex!=-1) { RoutePoint routePt = new RoutePoint(); int startIndex = currentIndex+routePtStart.Length; int endIndex = directions.IndexOf("'",startIndex); routePt.instruction = directions.Substring(startIndex, endIndex - startIndex); startIndex = directions.IndexOf(',', endIndex); startIndex++; endIndex = directions.IndexOf(',', startIndex); routePt.lat = decimal.Parse(directions.Substring(startIndex, endIndex-startIndex)); startIndex = directions.IndexOf(',', endIndex); startIndex++; endIndex = directions.IndexOf(',', startIndex); routePt.lon = decimal.Parse(directions.Substring(startIndex, endIndex-startIndex)); routePoints.Add(routePt); currentIndex = endIndex; } }while(currentIndex >0); return routePoints; } private string BuildParams() { string dirParams = string.Empty; dirParams += "&startlat=" + startLat + "&startlon=" + startLon; dirParams += "&endlat=" + endLat + "&endlon=" + endLon; return dirParams; } private HttpWebRequest CreateDirectionsRequest() { HttpWebRequest dirRequest = (HttpWebRequest) WebRequest.Create( "http://local.live.com/directions.ashx"); dirRequest.Method = "POST"; dirRequest.ServicePoint.Expect100Continue = false; dirRequest.ContentType = "application/x-www-form-urlencoded"; return dirRequest; } private string GetDirectionResults(HttpWebRequest dirRequest) { string results = string.Empty; HttpWebResponse dirResponse; dirResponse = (HttpWebResponse)dirRequest.GetResponse(); Stream receiveStream = dirResponse.GetResponseStream(); System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); StreamReader readStream = new StreamReader(receiveStream, encode); Char[] read = new Char[256]; // Reads 256 characters at a time. 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); } dirResponse.Close(); return results; } private string GetDirections() { string results = string.Empty; string dirParams = BuildParams(); HttpWebRequest dirRequest = CreateDirectionsRequest(); System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); byte[] bytes = encoding.GetBytes(dirParams); dirRequest.ContentLength = bytes.Length; Stream requestStream = dirRequest.GetRequestStream(); requestStream.Write(bytes, 0, bytes.Length); requestStream.Close(); try { results = GetDirectionResults(dirRequest); } catch(Exception ex) { Console.Out.WriteLine(ex.Message); } return results; } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } }