Using a TurnBox |
This section describes the use of a TurnBox to display turn information above a map. A TurnBox will draw the next turn that the navigator (driver) must make in order to navigate to the desired destination.
Note |
---|
We will modify the sample application (created in The IGps Interface tutorial) to use a TurnBox to display the upcoming turn event. |
This tutorial assumes basic knowledge of:
TurnBox implements the IMapRenderer interface, so to draw a TurnBox we can add the TurnBox to our map's RendererList. Add the following code to the form declarations (beneath the renderList):
TurnBox tBox = new TurnBox();
Remove the following line from your project.
IGps myGps;
Next, remove the GetGps() method in its entirety. This won't be needed for this example as we will create a new SimulatedGps in the buttonGo click event for our Navigator.
Add the following code to the declarations, beneath our TurnBox constructor:
private Navigator nav; String LangsPath = Settings.GeoBasePath("langs"); readonly private LatLon StartLocation = new LatLon(33.65856, -117.75528); readonly private LatLon DestinationLocation = new LatLon(33.65006, -117.75523);
Modify your UpdateLocation() method to match the code segment below. This ensures that the map is regularly updated with our current Navigator position and heading.
/* update current location of Navigator */ renderList.Add(new PushPin(nav.Gps.Position.location)); mapMain.Center = nav.Gps.Position.location; mapMain.Heading = nav.Gps.Position.heading; mapMain.Invalidate(); /* invalidating the map causes a redraw */
A TurnBox will be hidden from view if its CurrentEvent property is set to null.
We will show the TurnBox using a NotificationDelegate that will fire 100 meters before a turn event.
We will hide the TurnBox after each turn by using a second NotificationDelegate, firing on the Navigator object's AfterTurnDistanceNotification event.
Tip |
---|
The Navigator object's Destination property allows the Navigator to correctly determine the turn direction. |
You will need to add two methods to your code:
/* CLEAR TURNBOX */ private void ClearTurnBox(object sender, NavigationEvent e) { tBox.CurrentEvent = null; mapMain.Invalidate(); } /* SET TURNBOX */ private void SetTurnBox(object sender, NavigationEvent e) { tBox.CurrentEvent = e; mapMain.Invalidate(); }
Copy following lines of code over your buttonGo click event contents. This code in this segment will:
mapMain.Renderer = renderList; /* set map to render everything on our list */ mapMain.Zoom = 0.5; /* zoom the map close */ /* start a timer that fires every second, calling our eventhandler */ Timer tim = new Timer(); tim.Interval = 1000; tim.Tick += new EventHandler(UpdateLocation); tim.Start(); /* set up Navigator */ nav = new Navigator(new SimulatedGps(StartLocation), LangsPath, System.Globalization.CultureInfo.CurrentCulture); nav.Destination = new RouteStop(DestinationLocation); /* set up Navigator's notifications */ NotificationDelegate setDel = new NotificationDelegate(SetTurnBox); nav.AddNotification(new BeforeTurnDistanceNotification(100, 20, DistanceUnit.METERS, setDel)); NotificationDelegate clrDel = new NotificationDelegate(ClearTurnBox); nav.AddNotification(new AfterTurnDistanceNotification(25, DistanceUnit.METERS, clrDel));
Next, beneath this (still within the buttonGo_Click event), add the Turn box and the Navigator to the map's RendererList:
renderList.Add(tBox); renderList.Add(nav);
Add the following line of code to your UpdateLocation() method to ensure that the Navigator is updated with its current location.
nav.AddPoint(); /* update current location of Navigator */
Run the sample application. After you click buttonGo your application will, before a turn, draw a TurnBox showing the direction required to navigate towards the Navigator object's set Destination property. The TurnBox will be cleared after a turn has been made.