private static LatLng CalculateTrainPosistion(DateTime currentTime, Train train) { if (train.NextStopTime.ArrivalTime == DateTime.MinValue || train.LastStopTime.DepartureTime == DateTime.MinValue) { return(LatLng.Invalid); } var dif = train.NextStopTime.ArrivalTime.TimeOfDay - train.LastStopTime.DepartureTime.TimeOfDay; var currentDif = train.NextStopTime.ArrivalTime.TimeOfDay - currentTime.TimeOfDay; var percent = 1 - currentDif.TotalSeconds / dif.TotalSeconds; var distanceToStop = train.ShapesToNextStation.Last().ShapeDistTraveled; var distanceFromStop = train.ShapesToNextStation.First().ShapeDistTraveled; var distanceToNext = distanceToStop - distanceFromStop; var distanceTraveled = distanceToNext * percent; var trainPos = LatLng.Invalid; for (int s = 1; s < train.ShapesToNextStation.Count; s++) { var point = train.ShapesToNextStation[s]; if (point.ShapeDistTraveled > distanceFromStop + distanceTraveled) { var prevPoint = train.ShapesToNextStation[s - 1]; if (!prevPoint.ShapeDistTraveled.HasValue) { return(LatLng.Invalid); } var distanceSkiped = prevPoint.ShapeDistTraveled.Value - distanceFromStop.Value; var shapeDistanceTraveled = distanceTraveled - distanceSkiped; var shapeDistance = point.ShapeDistTraveled - prevPoint.ShapeDistTraveled; var percentDistance = shapeDistance.Value * shapeDistanceTraveled.Value / shapeDistance.Value; var p1 = new LatLng(prevPoint.ShapePtLat, prevPoint.ShapePtLon); var p2 = new LatLng(point.ShapePtLat, point.ShapePtLon); trainPos = Maths.ToBearingDistance(percentDistance, p1, Maths.CalculateBearing(p1, p2).ToRadians()); break; } } return(trainPos); }