I am having a table where the speed of the vehicle at the respective time are stored. Now I should calculate distance
the eg details of vehicle No(VNo), speed,datetime is given below
vno speed datetime
2117 0 9/12/07 1:00
2117 0 9/12/07 2:00
2117 0 9/12/07 3:00
2117 1 9/12/07 4:00
2117 2 9/12/07 5:00
2117 3 9/12/07 6:00
2117 4 9/12/07 7:00
2117 5 9/12/07 8:00
2117 6 9/12/07 9:00
2117 0 9/12/07 10:00
2117 0 9/12/07 11:00
2117 0 9/12/07 12:00
2117 0 9/12/07 13:00
2117 9 9/12/07 14:00
2117 6 9/12/07 15:00
2117 5 9/12/07 16:00
To calculate the distance you should take all the non zero speed values and if the value is
2117 9 9/12/07 14:00
2117 6 9/12/07 15:00
2117 5 9/12/07 16:00
then the start time is 9/12/07 14:00
and the endtime is 9/12/07 16:00
now calculate the time span take the sum of speed and calculate the distance
Now I have calculated the distance as below
Expand|Select|Wrap|Line Numbers
- protected void Button1_Click(object sender, EventArgs e)
- {
- MyCon.Open();
- SqlDataAdapter MyDa1 = new SqlDataAdapter("select speed,gps_datetime from gpsdata_history where registrationno='" + DropDownList1.SelectedValue + "'and (gps_datetime >='" + TextBox1.Text + "' and gps_datetime<='" + TextBox2.Text + "' )order by gps_datetime", MyCon);
- DataTable MyDt1 = new DataTable();
- MyDa1.Fill(MyDt1);
- MyCon.Close();
- int Speed = 0;
- int sumSpeed = 0;
- TimeSpan timediff = TimeSpan.Zero;
- int i = 1;
- DateTime Startdate;
- DateTime Enddate;
- DateTime chkend;
- chkend = DateTime.Now;
- double Totaltime = 0;
- int count = MyDt1.Rows.Count;
- for (i = 0; i < count; i++)
- {
- Speed = Convert.ToInt32(MyDt1.Rows[i][0].ToString());
- while (Speed == 0 && i < (count-1))
- {
- Speed = Convert.ToInt32(MyDt1.Rows[i]["speed"].ToString());
- i++;
- }
- Startdate = Convert.ToDateTime(MyDt1.Rows[i]["gps_datetime"].ToString());
- while (Speed > 0 && i < count)
- {
- Speed = Convert.ToInt32(MyDt1.Rows[i]["speed"].ToString());
- if (Speed != 0)
- {
- sumSpeed = sumSpeed + Convert.ToInt32(MyDt1.Rows[i]["speed"].ToString()); ;
- i++;
- }
- }
- i--;
- Enddate = Convert.ToDateTime(MyDt1.Rows[i]["gps_datetime"].ToString());
- timediff = timediff + (Enddate - Startdate);
- }
- Totaltime = timediff.TotalHours;
- double distance = Totaltime * sumSpeed;
- TextBox3.Text = Totaltime.ToString();
- TextBox4.Text = distance.ToString();
- TextBox5.Text = sumSpeed.ToString();
- }
regards
cmrhema