Using a LabelBox |
This section of the tutorial demonstrates the use of a LabelBox to display information above a map.
Note |
---|
We will modify the sample application created in the tutorial The IGps Interface to use a LabelBox to display the current heading, speed and GPS fix. |
The LabelBox class, like the PushPin in the previous tutorial, implements IMapRenderer. To draw a LabelBox we add the LabelBox to our RendererList.
The LabelBox has a parameterless constructor. Add the following snippet to your code such that the created LabelBox is global.
LabelBox lBox = new LabelBox();
The following code snippet will initialize the LabelBox to be positioned in the top right-hand corner of the map at a size of 150 x 150 pixels. Add this code snippet to your buttonGo click event.
/* setup labelbox - 150x100px, top RH corner */ lBox.Size = new System.Drawing.Size(150, 100); lBox.Top = mapMain.Top; lBox.Left = mapMain.Right - lBox.Width - 20; /* it won't be drawn until it's on the renderlist */ renderList.Add(lBox);
Modify your UpdateLocation() method to match the code segment below. This code segment is almost identical to that in the previous tutorial, with the addition of code to:
private void UpdateLocation(object sender, EventArgs e) { /* get the current heading ('N', 'NE' etc), speed (mph) and number of satellites in GPS fix */ string headAbbrev = Position.HeadingString(myGps.Position.heading); double mphSpeed = MathUtil.ConvertUnits(myGps.Position.speed, myGps.Position.units, SpeedUnit.MilesPerHour); int sats = myGps.Position.quality.NumSats; /* update labelbox, with heading & speed as major text, fix as minor text */ lBox.MajorText = string.Format("Traveling {0}\nSpeed: {1:##} mph", headAbbrev, mphSpeed); lBox.MinorText = string.Format("Fix from {0} satellites", sats); /* add pushpin (at current gps location) */ renderList.Add(new PushPin(myGps.Position.location)); /* update the map center and heading */ mapMain.Center = myGps.Position.location; mapMain.Heading = myGps.Position.heading; mapMain.Invalidate(); /* invalidating the map causes a redraw */ }
Run the sample application. After you click buttonGo your application will appear similar to the screenshot below.