====== Source Code ServicesEditionSample Compass CS 090820.zip ====== ====CompassAdornmentLayer.cs==== using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using ThinkGeo.MapSuite.Core; namespace Compass { public enum Position { UpperLeft, UpperRight, LowerLeft, LowerRight }; //This class accepts two images. One image for the needle (north arrow) and another optional image for //the frame of the compass. Typically, we want to have an image for the frame representing the angles (in degrees or other units) //representing to what angle the map is rotated. We could have a regular frame as in a traditional compass. //The image for the frame stays fixed as the map rotates whereas the image for the needle rotates with the map to always point //to the north. class CompassAdornmentLayer : AdornmentLayer { GeoImage needleImage = null; GeoImage frameImage = null; Position position; int sizePercentage; float rotateAngle; //Use that constructor with only one image for the needle (will work as a simple north arrow) public CompassAdornmentLayer(GeoImage needleImage, Position position, int sizePercentage) { this.needleImage = needleImage; this.position = position; this.sizePercentage = sizePercentage; } //Use that constructor with both images for needle and frame of the compass. public CompassAdornmentLayer(GeoImage needleImage, GeoImage frameImage, Position position, int sizePercentage) { this.needleImage = needleImage; this.frameImage = frameImage; this.position = position; this.sizePercentage = sizePercentage; } public GeoImage NeedleImage { get { return needleImage; } set { needleImage = value; } } public GeoImage FrameImage { get { return frameImage; } set { frameImage = value; } } //We needs to update that property as the map rotates to have the needle showing the north accurately. public float RotateAngle { get { return rotateAngle; } set { rotateAngle = value; } } public Position Position { get { return position; } set { position = value;} } public int SizePercentage { get { return sizePercentage; } set { sizePercentage = value; } } protected override void DrawCore(GeoCanvas canvas, Collection labelsInAllLayers) { //Draws the scaled images of needle and frame images according to the percentage size. For example with a PercentageSize of 80, //the compass size will be 80% of the size of needle image. //Sets the position of the compass. int margin = 10; int Xloc = 0; int Yloc = 0; int imageWidth = (sizePercentage * needleImage.GetWidth()) / 100; int imageHeight = (sizePercentage * needleImage.GetHeight()) / 100; switch (position) { case Position.UpperLeft: Xloc = (imageWidth /2)+ margin; Yloc = (imageHeight / 2) + margin; break; case Position.UpperRight: Xloc = ((int)canvas.Width -(imageWidth /2)) - margin; Yloc = (imageHeight / 2) + margin; break; case Position.LowerLeft: Xloc = (imageWidth / 2) + margin; Yloc = ((int)canvas.Height - (imageHeight / 2)) - margin; break; case Position.LowerRight: Xloc = ((int)canvas.Width - (imageWidth / 2)) - margin; Yloc = ((int)canvas.Height - (imageHeight / 2)) - margin; break; } //Draws the needle image at the size according to the percentage size and rotate the image according to RotateAngle property. canvas.DrawScreenImage(needleImage, Xloc, Yloc, imageWidth, imageHeight, DrawingLevel.LevelFour, 0, 0, rotateAngle); //If the frame image has been set, draws image at the size according to the percentage size with no rotation. if (frameImage != null) { canvas.DrawScreenImage(frameImage, Xloc, Yloc, imageWidth, imageHeight, DrawingLevel.LevelFour, 0, 0, 0); } } } } ====Program.cs==== using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace Compass { static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new TestForm()); } } } ====TestForm.cs==== using System; using System.Drawing; using System.Windows.Forms; using ThinkGeo.MapSuite.Core; namespace Compass { public partial class TestForm : Form { private MapEngine mapEngine = new MapEngine(); private Bitmap bitmap = null; private RotationProjection rotateProjection = new RotationProjection(); public TestForm() { InitializeComponent(); } private void TestForm_Load(object sender, EventArgs e) { mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-66.8581, 10.4995, -66.8456, 10.4915), Map.Width, Map.Height); mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.FromArgb(255, 233, 232, 214)); //Loads all the backgrounds layers for street, street label, park and park label. LoadBackgroundLayers(); //Makes sure all the layers have the Projection property set to RotationProjection. foreach (FeatureLayer Layer in mapEngine.StaticLayers) { Layer.FeatureSource.Projection = rotateProjection; } //Gets the image for the needle of the compass that will rotate with the map. GeoImage needleImage = new GeoImage(@"..\..\Data\compass_needle_letters_disc.png"); //Gets the image for the frame of the compass that will stay fixed. GeoImage frameImage = new GeoImage(@"..\..\Data\compass_circle_digits.png"); //Passes the images, the position on the map and the size of the compass in percentage to the size of the needle image. CompassAdornmentLayer compassAdornmentLayer = new CompassAdornmentLayer(needleImage, frameImage, Position.UpperLeft, 80); //Adds CompassAdornmentLayer for the compass to the AdormentLayers collection of mapEngine. mapEngine.AdornmentLayers.Add("MyCompassAdornmentLayer", compassAdornmentLayer); DrawImage(); } private void btnClockWise_Click(object sender, EventArgs e) { //Sets the Angle property of RotationProjection.Adds 10 to go clockwise. rotateProjection.Angle = rotateProjection.Angle + 10; //Sets CurrentExtent of mapEngine according to RotationProjection. mapEngine.CurrentExtent = rotateProjection.GetUpdatedExtent(mapEngine.CurrentExtent); //Updates RotateAngle property of CompassAdornmentLayer (Compass) according to the new value of //Angle property of RotationProjection. CompassAdornmentLayer compassAdornmentLayer = (CompassAdornmentLayer)mapEngine.AdornmentLayers[0]; compassAdornmentLayer.RotateAngle = (float)rotateProjection.Angle; DrawImage(); } private void btnCounterClockWise_Click(object sender, EventArgs e) { //Sets the Angle property of RotationProjection.Adds 10 to go clockwise. rotateProjection.Angle = rotateProjection.Angle - 10; //Sets CurrentExtent of mapEngine according to RotationProjection. mapEngine.CurrentExtent = rotateProjection.GetUpdatedExtent(mapEngine.CurrentExtent); //Updates RotateAngle property of CompassAdornmentLayer (Compass) according to the new value of //Angle property of RotationProjection. CompassAdornmentLayer compassAdornmentLayer = (CompassAdornmentLayer) mapEngine.AdornmentLayers[0]; compassAdornmentLayer.RotateAngle = (float)rotateProjection.Angle; DrawImage(); } private void DrawImage() { if (bitmap != null) { bitmap.Dispose(); } bitmap = new Bitmap(Map.Width, Map.Height); mapEngine.OpenAllLayers(); mapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree); mapEngine.DrawAdornmentLayers(bitmap, GeographyUnit.DecimalDegree); mapEngine.CloseAllLayers(); Map.Image = bitmap; } private void LoadBackgroundLayers() { ShapeFileFeatureLayer ParksShapeLayer = new ShapeFileFeatureLayer(@"..\..\Data\parks.shp"); ShapeFileFeatureLayer.BuildIndexFile(@"..\..\Data\parks.shp", BuildIndexMode.DoNotRebuild); ParksShapeLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; ParksShapeLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Park1; ShapeFileFeatureLayer ParksLabelLayer = new ShapeFileFeatureLayer(@"..\..\Data\parks.shp"); ParksLabelLayer.RequireIndex = false; ParksLabelLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.CreateSimpleTextStyle("Name", "Arial", 10, DrawingFontStyles.Bold, GeoColor.StandardColors.SeaGreen, GeoColor.StandardColors.White, 2); ParksLabelLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; ShapeFileFeatureLayer StreetsShapeLayer = new ShapeFileFeatureLayer(@"..\..\Data\streets.shp"); ShapeFileFeatureLayer.BuildIndexFile(@"..\..\Data\streets.shp", BuildIndexMode.DoNotRebuild); StreetsShapeLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; ValueStyle valueStyle = new ValueStyle(); valueStyle.ColumnName = "Type"; valueStyle.ValueItems.Add(new ValueItem("A", LineStyles.LocalRoad2)); valueStyle.ValueItems.Add(new ValueItem("M", LineStyles.MajorRoad1)); StreetsShapeLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(valueStyle); StreetsShapeLayer.FeatureSource.Projection = rotateProjection; ShapeFileFeatureLayer StreetsLabelLayer = new ShapeFileFeatureLayer(@"..\..\Data\streets.shp"); StreetsLabelLayer.RequireIndex = false; TextStyle textStyle = new TextStyle(); textStyle.TextColumnName = "St_name"; textStyle.TextSolidBrush = new GeoSolidBrush(GeoColor.StandardColors.Black); textStyle.Font = new GeoFont("Arial", 7); textStyle.TextLineSegmentRatio = 10; textStyle.GridSize = 100; textStyle.SplineType = SplineType.StandardSplining; textStyle.DuplicateRule = LabelDuplicateRule.OneDuplicateLabelPerQuadrant; StreetsLabelLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(textStyle); StreetsLabelLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle.GridSize = 5; StreetsLabelLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; StreetsLabelLayer.FeatureSource.Projection = rotateProjection; mapEngine.StaticLayers.Add("ParksShapeLayer", ParksShapeLayer); mapEngine.StaticLayers.Add("StreetsShapeLayer", StreetsShapeLayer); mapEngine.StaticLayers.Add("ParksLabelLayer", ParksLabelLayer); mapEngine.StaticLayers.Add("StreetsLabelLayer", StreetsLabelLayer); } 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(); } } }