User Tools

Site Tools


source_code_serviceseditionsample_flashingpointstyle_cs_091112.zip

Source Code ServicesEditionSample FlashingPointStyle CS 091112.zip

FlashingPointStyle.cs

 using System.Collections.Generic;  
 using ThinkGeo.MapSuite.Core;  
 
 namespace FlashingPointStyle  
 {  
     class FlashingPointStyle :  PointStyle  
     {  
         private GeoImage geoImage = null;  
         private bool inFlashingMode = false;  
         private bool drawIcon;  
 
         public GeoImage GeoImage  
         {  
             get { return geoImage; }  
             set { geoImage = value; }  
         }  
 
         public bool InFlashingMode  
         {  
             get { return inFlashingMode; }  
             set { inFlashingMode = value; }  
         }  
 
         protected override void DrawCore(IEnumerable<Feature> features, GeoCanvas canvas, System.Collections.ObjectModel.Collection<SimpleCandidate> labelsInThisLayer, System.Collections.ObjectModel.Collection<SimpleCandidate> labelsInAllLayers)  
         {  
             //Checks to see if in flashing mode or not.  
            if (inFlashingMode)  
             {  
                 if (drawIcon)  
                 {  
                     foreach (Feature feature in features)  
                     {  
                         PointShape centerPointShape = (PointShape)feature.GetShape();  
                         canvas.DrawWorldImageWithoutScaling(geoImage, centerPointShape.X, centerPointShape.Y, DrawingLevel.LabelLevel, 0, 0, RotationAngle);  
                     }  
                 }  
                 drawIcon = !drawIcon;  
             }  
             else  
             {  
                 foreach (Feature feature in features)  
                 {  
                     PointShape centerPointShape = (PointShape)feature.GetShape();  
                     canvas.DrawWorldImageWithoutScaling(geoImage, centerPointShape.X, centerPointShape.Y, DrawingLevel.LabelLevel, 0, 0, RotationAngle);  
                 }  
             }  
         }  
     }  
 }  
 

Program.cs

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Windows.Forms;  
 
 namespace FlashingPointStyle  
 {  
     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());  
         }  
     }  
 }  
 

TestForm.cs

 using System;  
 using System.Collections.ObjectModel;  
 using System.Data;  
 using System.Drawing;  
 using System.IO;  
 using System.Windows.Forms;  
 using ThinkGeo.MapSuite.Core;  
 
 namespace FlashingPointStyle  
 {  
     public partial class TestForm : Form  
     {  
         private Timer timer;  
         private Timer flashingTimer;  
         private StreamReader mGPSData = new StreamReader(@"..\..\data\GPSinfo.txt");  
         private double previousLong;  
         private double previousLat;  
         private MapEngine mapEngine = new MapEngine();  
         private MapEngine mapEngine2 = new MapEngine();  
         private Bitmap bitmap = null;  
         private Bitmap bitmap2 = null;  
         Collection<AreaBaseShape> zones = null;  
         FlashingPointStyle flashingPointStyle = null;  
 
         public TestForm()  
         {  
             InitializeComponent();  
             timer = new Timer();  
             flashingTimer = new Timer();  
         }  
 
         private void TestForm_Load(object sender, EventArgs e)  
         {  
             //Sets timers properties.  
             timer.Interval = 1500;  
             flashingTimer.Interval = 200;  
             timer.Tick += new EventHandler(timer_Tick);  
             flashingTimer.Tick += new EventHandler(flashingTimer_Tick);  
 
             // Sets the extent and the background color of the MapEngine for Static Layers .  
             mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-97.7591,30.3126,-97.7317,30.2964), Map.Width, Map.Height);  
             mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.StandardColors.LightGoldenrodYellow);  
 
             // Sets the extent and the background color of the MapEngine for vehicle dynamic layer .  
             mapEngine2.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-97.7591, 30.3126, -97.7317, 30.2964), Map.Width, Map.Height);  
             mapEngine2.BackgroundFillBrush = new GeoSolidBrush(GeoColor.StandardColors.Transparent);  
 
 
             //Displays the World Map Kit as a background.  
             ThinkGeo.MapSuite.Core.WorldMapKitLayer worldMapKitLayer = new ThinkGeo.MapSuite.Core.WorldMapKitLayer();  
             mapEngine.StaticLayers.Add(worldMapKitLayer);  
 
             //Creates the polygons for the prohibited zones.  
             PolygonShape polygonShape = new PolygonShape();  
             polygonShape.OuterRing.Vertices.Add(new Vertex(-97.7406,30.3057));  
             polygonShape.OuterRing.Vertices.Add(new Vertex(-97.7354,30.3031));  
             polygonShape.OuterRing.Vertices.Add(new Vertex(-97.7384,30.2979));  
             polygonShape.OuterRing.Vertices.Add(new Vertex(-97.7445,30.3006));  
             polygonShape.OuterRing.Vertices.Add(new Vertex(-97.7406,30.3057));  
 
             PolygonShape polygonShape2 = new PolygonShape();  
             polygonShape2.OuterRing.Vertices.Add(new Vertex(-97.747,30.3086));  
             polygonShape2.OuterRing.Vertices.Add(new Vertex(-97.745,30.3078));  
             polygonShape2.OuterRing.Vertices.Add(new Vertex(-97.7463,30.3057));  
             polygonShape2.OuterRing.Vertices.Add(new Vertex(-97.7486,30.3066));  
             polygonShape2.OuterRing.Vertices.Add(new Vertex(-97.747,30.3086));  
 
             //This collection is used in the timer_click to see if vehicle inside a zone or not.  
             zones = new Collection<AreaBaseShape>();  
             zones.Add(polygonShape);  
             zones.Add(polygonShape2);  
 
             //InMemoryFeatureLayer for zones.  
             InMemoryFeatureLayer zoneInMemoryFeatureLayer = new InMemoryFeatureLayer();  
             zoneInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(80, GeoColor.StandardColors.Red), GeoColor.StandardColors.Black);  
             zoneInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
             zoneInMemoryFeatureLayer.InternalFeatures.Add("Zone1", new Feature(polygonShape));  
             zoneInMemoryFeatureLayer.InternalFeatures.Add("Zone2", new Feature(polygonShape2));  
 
             //InMemoryFeatureLayer for vehicle.  
             InMemoryFeatureLayer inMemoryFeatureLayer = new InMemoryFeatureLayer();  
             //Adds FlashingPointStyle to CustomStyle.  
             flashingPointStyle = new FlashingPointStyle();  
             flashingPointStyle.GeoImage =  new GeoImage(@"..\..\data\sedan.png");  
             flashingPointStyle.RotationAngle = 45;  
             inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Clear();  
             inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(flashingPointStyle);  
             inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
             inMemoryFeatureLayer.InternalFeatures.Add("Car", new Feature(new PointShape()));  
 
             mapEngine.DynamicLayers.Add("ZoneLayer", zoneInMemoryFeatureLayer);  
 
             //Adds the InMemoryFeatureLayer for the car to the second mapengine to have it draw on separate bitmap  
             mapEngine2.DynamicLayers.Add("CarLayer", inMemoryFeatureLayer);  
 
             DrawImage();  
 
             timer.Start();  
         }  
 
         void flashingTimer_Tick(object sender, EventArgs e)  
         {  
            DrawImage2();  
         }  
 
         void timer_Tick(object sender, EventArgs e)  
         {  
             //Gets the GPS info from the textfile.  
             DataTable carData = GetCarData();  
 
             double angle;  
             InMemoryFeatureLayer inMemoryFeatureLayer =  (InMemoryFeatureLayer)mapEngine2.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"]);  
 
             //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));  
 
             flashingPointStyle.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;  
 
             //At each new vehicle location, it checks if the vehicle is inside one of the zones.  
             //If it is, it enables flashingTimer.  
             bool isInside = false;  
             foreach (AreaBaseShape areaBaseShape in zones)  
             {  
                 if (areaBaseShape.Contains(pointShape))  
                 {  
                     isInside = true;  
                     break;  
                 }  
             }  
 
             if (isInside == true)  
             {  
                 flashingTimer.Start();  
                 flashingPointStyle.InFlashingMode = true;  
             }  
 
             else  
             {  
                 flashingTimer.Stop();  
                 flashingPointStyle.InFlashingMode = false;  
             }  
 
             DrawImage2();  
         }  
 
         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 DrawImage2()  
         {  
             if (bitmap2 != null) { bitmap2.Dispose(); }  
             bitmap2 = new Bitmap(Map.Width, Map.Height);  
             mapEngine2.OpenAllLayers();  
             mapEngine2.DrawDynamicLayers(bitmap2, GeographyUnit.DecimalDegree);  
             mapEngine2.CloseAllLayers();  
 
             //Combines the bitmaps of the two mapengines to generate the new image of the picturebox.  
             Bitmap bitmap3 = new Bitmap(Map.Width, Map.Height);  
             Graphics g = Graphics.FromImage(bitmap3);  
             g.DrawImage(bitmap, 0, 0);  
             g.DrawImage(bitmap2, 0, 0);  
 
             Map.Image = bitmap3;  
         }  
 
         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 ToolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)  
         {  
             switch (e.Button.Tag.ToString())  
             {  
                 case "Zoom In":  
                     mapEngine.CurrentExtent.ScaleDown(50);  
                     mapEngine2.CurrentExtent.ScaleDown(50);  
                     break;  
                 case "Zoom Out":  
                     mapEngine.CurrentExtent.ScaleUp(50);  
                     mapEngine2.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);  
                     mapEngine2.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);  
                     mapEngine2.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Left, 20);  
                     break;  
                 case "Pan Right":  
                     mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Right, 20);  
                     mapEngine2.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Right, 20);  
                     break;  
                 case "Pan Up":  
                     mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Up, 20);  
                     mapEngine2.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Up, 20);  
                     break;  
                 case "Pan Down":  
                     mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Down, 20);  
                     mapEngine2.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);  
         }  
     }  
 }  
 
source_code_serviceseditionsample_flashingpointstyle_cs_091112.zip.txt · Last modified: 2015/09/08 08:01 by admin