private void TrackingStatusUpdated(object sender, RouteTrackerTrackingStatusChangedEventArgs e) { TrackingStatus status = e.TrackingStatus; if (status.DestinationStatus == DestinationStatus.NotReached || status.DestinationStatus == DestinationStatus.Approaching) { DistanceTB = status.RouteProgress.RemainingDistance.DisplayText; DistanceLbl = status.RouteProgress.RemainingDistance.DisplayTextUnits.PluralDisplayName; TimeTB = status.RouteProgress.RemainingTime.ToString(@"hh\:mm\:ss"); } else if (status.DestinationStatus == DestinationStatus.Reached) { this._dispatcher.Invoke(() => { isNavigating = false; ClearGraphics(); }); } if (_directionsList != null && status.CurrentManeuverIndex + 1 < _directionsList.Count) { DirectionManeuver dm = _directionsList[status.CurrentManeuverIndex + 1]; if (status.CurrentManeuverIndex + 1 < _directionsList.Count) { this._dispatcher.Invoke(() => { DirectionLbl.Text = dm.DirectionText; SetDirectionIcon(dm.ManeuverType.ToString()); }); } } }
private void UpdateDirection(DirectionManeuver direction) { if (direction == null) { LayoutRoot.Visibility = Visibility.Collapsed; return; } LayoutRoot.Visibility = Visibility.Visible; LayoutRoot.DataContext = direction; var d = LinearUnits.Miles.ConvertFromMeters(direction.Length); if (d == 0) { distance.Text = ""; } else if (d >= .25) { distance.Text = d.ToString("0.0 mi"); } else { d = LinearUnits.Yards.ConvertFromMeters(direction.Length); distance.Text = d.ToString("0 yd"); } if (direction.Duration.TotalHours >= 1) { time.Text = direction.Duration.ToString("hh\\:mm"); } else if (direction.Duration.TotalMinutes > 1) { time.Text = direction.Duration.ToString("mm\\:ss"); } else if (direction.Duration.TotalSeconds > 0) { time.Text = direction.Duration.ToString("ss") + " sec"; } else { time.Text = ""; } }
/// <summary> /// Call this to set your current location and update directions based on that. /// </summary> /// <param name="location"></param> public void SetCurrentLocation(MapPoint location) { List <string> propertyNames = new List <string>(new string[] { "NextManeuver", "WaypointLocation", "SnappedLocation", "CurrentDirection", "TimeToWaypoint", "DistanceToDestination", "DistanceToWaypoint", "TimeToDestination", "MilesToDestination", "MilesToWaypoint", }); DirectionManeuver closest = null; double distance = double.NaN; MapPoint snappedLocation = null; Route direction = null; // Find the route part that we are currently on by snapping to each segment and see which one is the closest foreach (var dir in m_route.Routes) { var closestCandidate = (from a in dir.DirectionManeuvers where a.Geometry is Polyline select new { Direction = a, Proximity = GeometryEngine.NearestCoordinate(a.Geometry, location) }).OrderBy(b => b.Proximity.Distance).FirstOrDefault(); if (double.IsNaN(distance) || distance < closestCandidate.Proximity.Distance) { distance = closestCandidate.Proximity.Distance; closest = closestCandidate.Direction; snappedLocation = closestCandidate.Proximity.Coordinate; direction = dir; } } if (closest != null) { var directions = direction.DirectionManeuvers.ToList(); var idx = directions.IndexOf(closest); if (idx < directions.Count) { DirectionManeuver next = directions[idx + 1]; //calculate how much is left of current route segment var segment = closest.Geometry as Polyline; var proximity = GeometryEngine.NearestVertex(segment, snappedLocation); double frac = 1 - GetFractionAlongLine(segment, proximity, snappedLocation); TimeSpan timeLeft = new TimeSpan((long)(closest.Duration.Ticks * frac)); double segmentLengthLeft = (Convert.ToDouble(closest.Length)) * frac; //Sum up the time and lengths for the remaining route segments TimeSpan totalTimeLeft = timeLeft; double totallength = segmentLengthLeft; for (int i = idx + 1; i < directions.Count; i++) { totalTimeLeft += directions[i].Duration; totallength += directions[i].Length; } //Update properties TimeToWaypoint = TimeSpan.FromSeconds(Math.Round(timeLeft.TotalSeconds)); TimeToDestination = TimeSpan.FromSeconds(Math.Round(totalTimeLeft.TotalSeconds)); DistanceToWaypoint = Math.Round(segmentLengthLeft); DistanceToDestination = Math.Round(totallength); SnappedLocation = snappedLocation; var maneuverType = next.ManeuverType; WaypointLocation = segment.Parts.Last().LastOrDefault().EndPoint; #if NETFX_CORE || WINDOWS_PHONE var maneuverUri = new Uri(string.Format("ms-appx:///Assets/Maneuvers/{0}.png", maneuverType)); #else var maneuverUri = new Uri(string.Format("pack://application:,,,/Assets/Maneuvers/{0}.png", maneuverType)); #endif if (ManeuverImage != maneuverUri) { ManeuverImage = maneuverUri; propertyNames.Add("ManeuverImage"); } NextManeuver = next.DirectionText; RaisePropertiesChanged(propertyNames); } } }