Get Directions

Topics: General
Oct 25, 2010 at 11:05 AM

Is posible to create a route log text?

Something like this...

===============================================================

Indicaciones de ruta en coche para Calle de Aguado, 18, 33202 Gijón
Plaza de San Miguel
1. Dirígete hacia el oeste en Plaza de San Miguel hacia Calle Capua
180 m
2. Sal de la rotonda en Calle de Ruiz Gómez
110 m
3. Gira a la derecha en Calle Marqués de Casa Valdés
500 m
4. Gira a la izquierda en Calle de Aguado
El destino está a la derecha.
110 m
Calle de Aguado, 18
33202 Gijón

===========================================================

Many Thanks and Best Regards

Coordinator
Oct 25, 2010 at 11:12 AM

yes, there is incomplete function GetRouteBetweenPointsKmlUrl, so you need manually parse kml

Oct 25, 2010 at 11:57 AM

Hi jcferalva.

This ain't the GetRouteBetweenPointsKmlUrl method but sofar its been working for me.

 
public partial class frmDirections : Form
{
	ArrayList dirs = new ArrayList();
	PointLatLng start;
	PointLatLng end;
	
	public frmDirections(PointLatLng startLoc, PointLatLng endLoc)
	{
		InitializeComponent();
		start = startLoc;
		end = endLoc;

		pBarLoading.Visible = true;
		backgroundWorker1.RunWorkerAsync();
	}

	private ArrayList getDirections(PointLatLng start, PointLatLng end)
	{
		ArrayList dirs = new ArrayList();

		GISCoordiante dmsLAT = new GISCoordiante(start.Lat, GISCoordiante.CoordinateType.latitude);
		GISCoordiante dmsLNG = new GISCoordiante(start.Lng, GISCoordiante.CoordinateType.longitude);

		string fromAddr = dmsLAT.toDMSString() + " " + dmsLNG.toDMSString();

		dmsLAT = new GISCoordiante(end.Lat, GISCoordiante.CoordinateType.latitude);
		dmsLNG = new GISCoordiante(end.Lng, GISCoordiante.CoordinateType.longitude);
		string toAddr = dmsLAT.toDMSString() + " " + dmsLNG.toDMSString();

		string html = "";

		string startStr = "<td class=\"dirsegtext dir-step-show\" id=dirsegtext_0_";
		string checkStr = "";
		string endStr = "</td><td><img id=cbicon_0_";
		string dir = "";
		int startIndex = 1;
		int stopIndex = 0;
		int index = 0;

		WebClient strWebClient = new WebClient();
		string pageUrl = "http://maps.google.com/";


		//Build the page request
		pageUrl = pageUrl + "maps?saddr=" + fromAddr + "&daddr=" + toAddr;

		//Get the Page Text
		html = strWebClient.DownloadString(pageUrl);

		//Build the directions list
		startIndex = html.IndexOf(startStr) + startStr.Length;
		stopIndex = html.IndexOf(endStr, startIndex);

		while (startIndex > 0)
		{
			checkStr = startStr + index + ">";
			startIndex = html.IndexOf(checkStr) + checkStr.Length;
			stopIndex = html.IndexOf(endStr, startIndex);

			if (stopIndex > startIndex)
			{
				//(index + 1).ToString() + ": " + '
				dir = (index + 1).ToString() + ": " + html.Substring(startIndex, (stopIndex - startIndex)) + ".";

				dirs.Add(StripHTML(dir));
				html = html.Substring(stopIndex);
			}
			else
			{ startIndex = -1; }

			index++;
		}

		return dirs;
	}

	static string StripHTML(string inputString)
	{
		return Regex.Replace(inputString.Replace("&nbsp;", " ").Replace("/", " ").Replace("<div", ". <div"), "<.*?>", string.Empty);
	}

	private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
	{
		dirs = getDirections(start, end);
	}

	private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
	{
		lstDirections.Items.Clear();

		if (dirs.Count > 0)
		{
			foreach (string dir in dirs)
			{
				lstDirections.Items.Add(dir);
			}
		}
		else
		{
			lstDirections.Items.Add("No Directions Found.");
		}

		pBarLoading.Visible = false;
	}
}
    


/* GIS COORDINATE CLASS */

 public class GISCoordiante
{
	#region Enums

	public enum CoordinateType
	{
		longitude,
		latitude
	}

	#endregion

	#region Variables & Properties

	private double degrees;
	public double Degrees
	{
		get { return degrees; }
		set { degrees = value; }
	}

	private double minutes;
	public double Minutes
	{
		get { return minutes; }
		set { minutes = value; }
	}

	private double seconds;
	public double Seconds
	{
		get { return seconds; }
		set { seconds = value; }
	}

	private char heading;
	public char Heading
	{
		get { return heading; }
		set { heading = value; }
	}

	private string dmsString;
	public string DMSString
	{
		get { return dmsString; }
		set { dmsString = value; }
	}
	#endregion

	#region Constructors

	/// <summary>
	/// Empty Constructor
	/// </summary>
	public GISCoordiante()
	{ }

	/// <summary>
	/// DegreesMinutesSeconds Constructor
	/// </summary>
	/// <param name="coordinate">Decimal Coordinate</param>
	/// <param name="type">CoordinateType longitude or latitude </param>
	public GISCoordiante(double coordinate, CoordinateType type)
	{
		// Set flag if number is negative
		bool neg = coordinate < 0d;

		// Work with a positive number
		coordinate = Math.Abs(coordinate);

		// Get d/m/s components
		double d = Math.Floor(coordinate);
		coordinate -= d;
		coordinate *= 60;
		Degrees = d;

		double m = Math.Floor(coordinate);
		coordinate -= m;
		coordinate *= 60;
		Minutes = m;

		double s = Math.Round(coordinate);
		Seconds = s;

		// Create padding character
		char pad;
		char.TryParse("0", out pad);

		// Create d/m/s strings
		string dd = d.ToString();
		string mm = m.ToString().PadLeft(2, pad);
		string ss = s.ToString().PadLeft(2, pad);

		// Append d/m/s
		DMSString = string.Format("{0}�{1}'{2}\"", dd, mm, ss);

		// Append compass heading
		switch (type)
		{
			case CoordinateType.longitude:
				DMSString += neg ? "W" : "E";
				Heading = neg ? 'W' : 'E';
				break;
			case CoordinateType.latitude:
				DMSString += neg ? "S" : "N";
				Heading = neg ? 'S' : 'N';
				break;
		}
	}

	#endregion

	#region Methods

	public string toDMSString()
	{
		return DMSString;
	}

	#endregion
}

Coordinator
Sep 20, 2011 at 2:53 PM

direction provider now is implemented for google map