Distance comparer


/ Published in: C#
Save to your folder(s)

Application to calculate the distance between serveral adresses


Copy this code and paste it in your HTML
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Threading;
  8. using System.Windows.Forms;
  9. using System.Xml;
  10. using GMap.NET;
  11. using GMap.NET.MapProviders;
  12. using GMap.NET.WindowsForms;
  13.  
  14.  
  15. namespace Gmap
  16. {
  17. public partial class Form1 : Form
  18. {
  19.  
  20. public Form1()
  21. {
  22. InitializeComponent();
  23.  
  24.  
  25. }
  26.  
  27.  
  28. //calculate distance
  29. public void button1_Click(object sender, EventArgs e)
  30. {
  31. dataGridView1.Rows.Clear();
  32. //var
  33. TextReader tr = new StreamReader("addreses.txt");
  34. GDirections gdr;
  35. string line;
  36. var x = DirectionsStatusCode.ZERO_RESULTS;
  37. while ((line = tr.ReadLine()) != null)
  38. {
  39. string adrs = tr.ReadLine();
  40. x = GMapProviders.GoogleMap.GetDirections(out gdr, textBox1.Text, adrs, false, false, false, true, true);
  41. if (x == DirectionsStatusCode.OK)
  42. {
  43. dataGridView1.Rows.Add(line, gdr.Distance);
  44. }
  45. else
  46. {
  47. dataGridView1.Rows.Add("Addres is unknown", "Please change addres");
  48. }
  49. }
  50. tr.Close();
  51. }
  52.  
  53. //save and load
  54. private void button2_Click(object sender, EventArgs e)
  55. {
  56.  
  57.  
  58. if (button2.Text == "save")
  59. {
  60. TextWriter tw = new StreamWriter("addreses.txt", false);
  61. for (int i = 0; dataGridView1.RowCount > i; i++)
  62. {
  63. for (int a = 0; a < 2; a++)
  64. {
  65. if (dataGridView1[a, i].Value != "" && dataGridView1[a, i].Value != null)
  66. {
  67. tw.WriteLine(Convert.ToString(dataGridView1[a, i].Value));
  68. }
  69. }
  70. }
  71. tw.Close();
  72. MessageBox.Show("data has been updated");
  73. button2.Text = "Edit adresses";
  74. dataGridView1.Rows.Clear();
  75. dataGridView1.Columns[1].HeaderText = "Distance";
  76. button1.Enabled = true;
  77. dataGridView1.ReadOnly = true;
  78. googleMapsToolStripMenuItem.Enabled = true;
  79. }
  80.  
  81. else//Edit addreses
  82. {
  83. googleMapsToolStripMenuItem.Enabled = false;
  84. dataGridView1.Rows.Clear();
  85. dataGridView1.Columns[1].HeaderText = "Addres";
  86. button2.Text = "save";
  87. dataGridView1.ReadOnly = false;
  88. button1.Enabled = false;
  89. //load
  90. try
  91. {
  92. TextReader tr = new StreamReader("addreses.txt");
  93.  
  94. string line;
  95. while ((line = tr.ReadLine()) != null)
  96. {
  97. dataGridView1.Rows.Add(line, tr.ReadLine());
  98. }
  99. tr.Close();
  100. }
  101. catch { }
  102. }
  103. }
  104.  
  105. //clear on click
  106. private void textBox1_Enter(object sender, EventArgs e)
  107. {
  108. if (textBox1.Text == "Addres")
  109. textBox1.Text = null;
  110. }
  111.  
  112. //Delete row
  113. private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
  114. {
  115. try
  116. {
  117. dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
  118. }
  119. catch
  120. { }
  121. }
  122.  
  123. //on enter press
  124. private void enter(object sender, KeyPressEventArgs e)
  125. {
  126. if (e.KeyChar == (char)13)
  127. {
  128. button1_Click(button1, e);
  129. }
  130. }
  131.  
  132.  
  133. private void googleMapsToolStripMenuItem_Click(object sender, EventArgs e)
  134. {
  135. string web;
  136.  
  137. StreamReader sr = new StreamReader("addreses.txt");
  138. string line;
  139.  
  140. while ((line = sr.ReadLine()) != dataGridView1[0, dataGridView1.CurrentRow.Index].Value)
  141. {
  142.  
  143. if(line.Equals(dataGridView1[0, dataGridView1.CurrentRow.Index].Value) == true)
  144. {
  145. break;
  146. }
  147.  
  148. }
  149.  
  150. web = sr.ReadLine();
  151. sr.Close();
  152.  
  153. System.Diagnostics.Process.Start("https://maps.google.com/maps?q=" + web + "+to+" + textBox1.Text);
  154.  
  155.  
  156.  
  157. }
  158. }
  159. }
  160. //Created by Joeri Lock

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.