Tracking.cs

This example demonstrates how to visualize a moving vehicle on the map. The examples loads the layers with roads and buildings. A path for a vehicle is opened a separate shapefile with a single closed polyline shape. It is not added to the map. On the events generated by timer a new position of the vehicle is calculated assuming the constant speed. Then a red dot is displayed on the drawing layer on every even occurrence of the event (0, 2, 4, etc). The drawing layer is cleared on every new occurrence of the event. The usage of the drawing layer helps to avoid complete redraws of the map which can be slow. Here is a screenshot with the results of the code execution.

using System;
using System.IO;
using System.Windows.Forms;
using AxMapWinGIS;
using MapWinGIS;
namespace Examples
{
public partial class MapExamples
{
private double m_distance = 0.0; // the distance passed
private double m_step = 10.0; // the distance to pass on a single timer event
private Shape m_path = null; // the shape which holds the path of vehicle
private int m_count = 0; // number of steps performed
public Timer m_timer = new Timer();
// <summary>
// Loads the layers, registers event handlers
// </summary>
public void Tracking(AxMap axMap1, string dataPath)
{
axMap1.Projection = tkMapProjection.PROJECTION_NONE;
axMap1.GrabProjectionFromData = true;
axMap1.DisableWaitCursor = true;
string filename1 = dataPath + "buildings.shp";
string filename2 = dataPath + "roads.shp";
string filename3 = dataPath + "path.shp";
if (!File.Exists(filename1) || !File.Exists(filename2) || !File.Exists(filename3))
{
MessageBox.Show("Couldn't find the files (buildings.shp, roads.shp, path.shp): " + dataPath);
}
else
{
Shapefile sf = new Shapefile();
sf.Open(filename1, null);
axMap1.AddLayer(sf, true);
sf = new Shapefile();
sf.Open(filename2, null);
sf.Labels.Generate("[Name]", tkLabelPositioning.lpLongestSegement, false);
Utils utils = new Utils();
LinePattern pattern = new LinePattern();
pattern.AddLine(utils.ColorByName(tkMapColor.Brown), 10.0f, tkDashStyle.dsSolid);
pattern.AddLine(utils.ColorByName(tkMapColor.Yellow), 9.0f, tkDashStyle.dsSolid);
axMap1.AddLayer(sf, true);
sf = new Shapefile();
sf.Open(filename3, null);
m_path = sf.Shape[0];
axMap1.MapUnits = tkUnitsOfMeasure.umMeters;
axMap1.CurrentScale = 5000.0;
m_timer.Interval = 250;
m_timer.Tick += TimerTick;
m_timer.Start();
}
}
// <summary>
// Calculates the new position
// </summary>
void TimerTick(object sender, EventArgs e)
{
// moves car a step further
m_distance += m_step;
if (m_distance > m_path.Length)
m_distance = m_path.Length - m_distance;
//calculating the current position (x2, y2)
double distance = 0.0;
double x1, x2, y1, y2;
x1= x2 = y1 = y2 = 0.0;
for (int i = 1; i < m_path.numPoints; i++)
{
m_path.get_XY(i, ref x2, ref y2);
m_path.get_XY(i - 1, ref x1, ref y1);
double val = Math.Sqrt(Math.Pow(x2 - x1, 2.0) + Math.Pow(y2 - y1, 2.0));
if (distance + val > m_distance)
{
double ratio = (m_distance - distance) / val;
x2 = x1 + (x2 - x1) * ratio;
y2 = y1 + (y2 - y1) * ratio;
//distance += val * ratio;
break;
}
if (distance + val < m_distance)
{
distance += val;
}
else
{
break;
}
}
this.DrawPosition(x2, y2);
}
// <summary>
// Displays the point in the current position
// </summary>
private void DrawPosition(double x, double y)
{
try
{
axMap1.ClearDrawings();
Extents ext = axMap1.Extents as Extents;
if (x < ext.xMin || x > ext.xMax || y < ext.yMin || y > ext.yMax)
{
double width = (ext.xMax - ext.xMin) / 2.0;
double height = (ext.yMax - ext.yMin) / 2.0;
ext.SetBounds(x - width, y - height, 0.0, x + width, y + height, 0.0);
axMap1.Extents = ext;
Application.DoEvents();
}
if (m_count % 2 == 0)
{
int handle = axMap1.NewDrawing(tkDrawReferenceList.dlScreenReferencedList);
double pxX = 0.0;
double pxY = 0.0;
axMap1.ProjToPixel(x, y, ref pxX, ref pxY);
axMap1.DrawCircleEx(handle, pxX, pxY, 5.0, 255, true);
}
m_count++;
}
catch
{
// the function can be called when the form is about to close
}
}
}
}
tkLabelPositioning
The available positioning of the label relative to the parent shape.
Definition: Enumerations.cs:835
tkMapColor
A list of named constants for some of the well-known colors.
Definition: Enumerations.cs:951
tkDashStyle
The available style of lines. Can be used for drawing polylines and outlines of the polygons.
Definition: Enumerations.cs:442
tkMapProjection
Commonly used map projections to be set in Form Designer (see AxMap.Projection property).
Definition: Enumerations.cs:1741
tkUnitsOfMeasure
The possible units of measure for the data being displaying on map.
Definition: Enumerations.cs:1397
tkDrawReferenceList
The type of spatial reference for the drawing layer.
Definition: Enumerations.cs:484
Map component for visualization of vector, raster or grid data.
Definition: AxMap.cs:56
Represents a rectangle on the map.
Definition: Extents.cs:49
double xMax
The maximum x bound for the extents object.
Definition: Extents.cs:121
double yMax
The maximum y bound for the extents object.
Definition: Extents.cs:137
void SetBounds(double xMin, double yMin, double zMin, double xMax, double yMax, double zMax)
Sets the bounds for the extents object.
Definition: Extents.cs:85
double xMin
Gets the minimum x bound for the extents object.
Definition: Extents.cs:129
double yMin
Gets the minimum y bound for the extents object
Definition: Extents.cs:145
Provides means for defining custom pattern from lines and point symbols for rendering polyline layers...
Definition: LinePattern.cs:46
void AddLine(uint Color, float Width, tkDashStyle style)
Adds a line segment to the pattern.
Definition: LinePattern.cs:54
LinePattern LinePattern
Gets or sets line pattern for rendering polyline shapefile.
Definition: ShapeDrawingOptions.cs:586
bool UseLinePattern
Gets or set the value which indicates whether line pattern will be used to render polyline shapefile.
Definition: ShapeDrawingOptions.cs:790
A shape object represents a geometric shape which can be added to a shapefile which is displayed in t...
Definition: Shape.cs:41
bool get_XY(int pointIndex, ref double x, ref double y)
Gets the coordinates of the specified point.
Definition: Shape.cs:639
double Length
Calculates the length of polyline shape.
Definition: Shape.cs:446
Provides a functionality for accessing and editing ESRI shapefiles.
Definition: Shapefile.cs:72
Labels Labels
Gets or sets the instance of the Labels class associated with the shapefile.
Definition: Shapefile.cs:184
ShapeDrawingOptions DefaultDrawingOptions
Gets or sets an instance of ShapeDrawingOptions class which holds default drawing options.
Definition: Shapefile.cs:111
A utils object provides access to a set of utility functions to perform a variety of tasks on other o...
Definition: Utils.cs:20
uint ColorByName(tkMapColor Name)
Returns the numeric representation for the specified color.
Definition: Utils.cs:40
int Generate(string Expression, tkLabelPositioning Method, bool LargestPartOnly)
Generates labels for each shape of the parent shapefile.
Definition: Labels.cs:490
tkUnitsOfMeasure MapUnits
Gets or sets the units of measure for the map.
Definition: AxMap.cs:2622
void ProjToPixel(double projX, double projY, ref double pixelX, ref double pixelY)
Converts projected map coordinates into screen pixel units
Definition: AxMap.cs:2657
tkMapProjection Projection
Sets projection of the map. It providers 2 most commonly used coordinate system/projections to be eas...
Definition: AxMap.cs:2709
bool GrabProjectionFromData
Gets or sets a value indicating whether projection for will be taken from the first datasource added ...
Definition: AxMap.cs:2701
void DrawCircleEx(int layerHandle, double x, double y, double pixelRadius, uint color, bool fill, byte alpha=255)
Draws a circle on the specified drawing layer.
Definition: AxMap.cs:1708
void ClearDrawings()
Clears all drawings on all drawing layers, and removes all drawing layers. This method is slower than...
Definition: AxMap.cs:1679
int NewDrawing(tkDrawReferenceList projection)
Creates a new drawing layer on the map returning its handle.
Definition: AxMap.cs:1868
double CurrentScale
Gets or sets the current map scale.
Definition: AxMap.cs:2395
Extents Extents
Gets or sets the extents of the map using an Extents object.
Definition: AxMap.cs:2426
bool DisableWaitCursor
Gets or sets a boolean value which indicates whether a wait cursor will be displayed on map redraw.
Definition: AxMap.cs:533
int AddLayer(object Object, bool visible)
Adds a layer to the map.
Definition: AxMap.cs:1342
bool Open(string shapefileName, ICallback cBack)
Opens shapefile from the disk.
Definition: Shapefile.cs:1430