public double CalculateSpeed() { TimeSpan elapsedTime = DateTime.UtcNow - initialDate; GeoLocation GL = new GeoLocation(); GL.Location(); /*dlon = lon2 - lon1 * dlat = lat2 - lat1 * a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2 * c = 2 * atan2( sqrt(a), sqrt(1-a) ) * d = R * c (where R is the radius of the Earth)*/ lat1 = DegreeToRadian(ps.getInitialLatitude()); lat2 = DegreeToRadian(GL.getLatitude()); lon1 = DegreeToRadian(ps.getInitialLongitude()); lon2 = DegreeToRadian(GL.getLongitude()); double speed; /* * MessageBox.Show("1 " + lat1 + " 2 " + lon1); * MessageBox.Show("1 " + lat2 + " 2 " + lon2); * MessageBox.Show(elapsedTime.TotalHours.ToString()); */ if (Math.Round(lat1, 3) == Math.Round(lat2, 3) && Math.Round(lon1, 3) == Math.Round(lon2, 3)) { speed = 0; } else { double dlon = lon2 - lon1; double dlat = lat2 - lat1; double a = Math.Pow((Math.Sin(dlat / 2)), 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Pow((Math.Sin(dlon / 2)), 2); double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); double distance = 3961 * c; //3961 miles = Radius of the Earth. It is equivalent to 6373 km // miles / second speed = distance / elapsedTime.TotalHours; // return Math.Round(speed,2); // MessageBox.Show(elapsedTime.TotalHours.ToString()); } return(speed); }