ThinkGeo Cloud
ThinkGeo UI Controls
ThinkGeo Open Source
Help and Support
External Resources
ThinkGeo Cloud
ThinkGeo UI Controls
ThinkGeo Open Source
Help and Support
External Resources
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace CenteringOnMovingFeature { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new TestForm()); } } }
using System; using System.Collections.ObjectModel; using System.Drawing; using System.Windows.Forms; using System.IO; using System.Data; using ThinkGeo.MapSuite.Core; namespace CenteringOnMovingFeature { public partial class TestForm : Form { private MapEngine mapEngine = new MapEngine(); private Bitmap bitmap = null; private StreamReader mGPSData = new StreamReader(@"..\..\data\GPSinfo.txt"); private Timer timer; private double previousLong; private double previousLat; public TestForm() { InitializeComponent(); timer = new Timer(); } private void TestForm_Load(object sender, EventArgs e) { //Sets timers properties. timer.Interval = 2000; timer.Tick += new EventHandler(timer_Tick); // Set the extent and the background color mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-97.7591, 30.3126, -97.7317, 30.2964), Map.Width, Map.Height); mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean); //Displays the World Map Kit as a background. ThinkGeo.MapSuite.Core.WorldMapKitLayer worldMapKitLayer = new ThinkGeo.MapSuite.Core.WorldMapKitLayer(); mapEngine.StaticLayers.Add(worldMapKitLayer); //InMemoryFeatureLayer for vehicle. InMemoryFeatureLayer inMemoryFeatureLayer = new InMemoryFeatureLayer(); inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.PointType = PointType.Bitmap; inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.Image = new GeoImage(@"..\..\data\sedan.png"); inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.RotationAngle = 45; inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; inMemoryFeatureLayer.InternalFeatures.Add("Car", new Feature(new PointShape())); //Adds the InMemoryFeatureLayer for the car to the second mapengine to have it draw on separate bitmap mapEngine.DynamicLayers.Add("CarLayer", inMemoryFeatureLayer); DrawImage(); timer.Start(); } void timer_Tick(object sender, EventArgs e) { //Gets the GPS info from the textfile. DataTable carData = GetCarData(); double angle; InMemoryFeatureLayer inMemoryFeatureLayer = (InMemoryFeatureLayer)mapEngine.DynamicLayers["CarLayer"]; PointShape pointShape = inMemoryFeatureLayer.InternalFeatures[0].GetShape() as PointShape; // Get the Row of Data we are working with. DataRow carDataRow = carData.Rows[0]; double Lat = Convert.ToDouble(carDataRow["LAT"]); double Long = Convert.ToDouble(carDataRow["LONG"]); if (previousLong == 0) { previousLong = Long; previousLat = Lat; } double Xdiff = previousLong - Long; double Ydiff = previousLat - Lat; //Gets the angle based on the current GPS position and the previous one to get the direction of the vehicle. angle = GetAngleFromTwoVertices(new Vertex(previousLong, previousLat), new Vertex(Long, Lat)); inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.RotationAngle = 90 - (float)angle; pointShape.X = Long; pointShape.Y = Lat; pointShape.Id = "Car"; inMemoryFeatureLayer.Open(); inMemoryFeatureLayer.EditTools.BeginTransaction(); inMemoryFeatureLayer.EditTools.Update(pointShape); inMemoryFeatureLayer.EditTools.CommitTransaction(); inMemoryFeatureLayer.Close(); previousLong = Long; previousLat = Lat; //Function to center the map to a point (the moving moving vehicle feature) mapEngine.CenterAt(new PointShape(Long, Lat), Map.Width, Map.Height); DrawImage(); } private DataTable GetCarData() { DataTable datatable = new DataTable(); datatable.Columns.Add("LAT"); datatable.Columns.Add("LONG"); string strLattitude = ""; string strLongitude = ""; // Read the next line from the text file with GPS data in it. string strCurrentText = mGPSData.ReadLine(); if (strCurrentText == "") { mGPSData.BaseStream.Seek(0, SeekOrigin.Begin); strCurrentText = mGPSData.ReadLine(); } while (strCurrentText != null) { // Every other line is a "/" and we want to skip those. if (strCurrentText.Trim() != "/") { string[] strSplit = strCurrentText.Split(','); // (':'); strLongitude = strSplit[0]; strLattitude = strSplit[1]; break; } strCurrentText = mGPSData.ReadLine(); } object[] objs = new object[2] { strLattitude, strLongitude }; datatable.Rows.Add(objs); return datatable; } //We assume that the angle is based on a third point that is on top of b on the same x axis. private double GetAngleFromTwoVertices(Vertex b, Vertex c) { double alpha = 0; double tangentAlpha = (c.Y - b.Y) / (c.X - b.X); double Peta = Math.Atan(tangentAlpha); if (c.X > b.X) { alpha = 90 - (Peta * (180 / Math.PI)); } else if (c.X < b.X) { alpha = 270 - (Peta * (180 / Math.PI)); } else { if (c.Y > b.Y) alpha = 0; if (c.Y < b.Y) alpha = 180; } return alpha; } private void DrawImage() { if (bitmap != null) { bitmap.Dispose(); } bitmap = new Bitmap(Map.Width, Map.Height); mapEngine.OpenAllLayers(); mapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree); mapEngine.DrawDynamicLayers(bitmap, GeographyUnit.DecimalDegree); mapEngine.CloseAllLayers(); Map.Image = bitmap; } private void ToolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e) { switch (e.Button.Tag.ToString()) { case "Zoom In": mapEngine.CurrentExtent.ScaleDown(50); break; case "Zoom Out": mapEngine.CurrentExtent.ScaleUp(50); break; case "Full Extent": mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-180.0, 83.0, 180.0, -90.0), Map.Width, Map.Height); break; case "Pan Left": mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Left, 20); break; case "Pan Right": mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Right, 20); break; case "Pan Up": mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Up, 20); break; case "Pan Down": mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Down, 20); break; default: break; } DrawImage(); } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } private void Map_MouseMove(object sender, MouseEventArgs e) { //Displays the X and Y in screen coordinates. statusStrip1.Items["toolStripStatusLabelScreen"].Text = "X:" + e.X + " Y:" + e.Y; //Gets the PointShape in world coordinates from screen coordinates. PointShape pointShape = ExtentHelper.ToWorldCoordinate(mapEngine.CurrentExtent, new ScreenPointF(e.X, e.Y), Map.Width, Map.Height); //Displays world coordinates. statusStrip1.Items["toolStripStatusLabelWorld"].Text = "(world) X:" + Math.Round(pointShape.X, 4) + " Y:" + Math.Round(pointShape.Y, 4); } } }