====== Source Code ServicesEditionSample MapShapes CS 090728.zip ====== ====MapShape.cs==== using System; using System.Collections.Generic; using System.Linq; using System.Text; using ThinkGeo.MapSuite.Core; namespace MapShapes { // This is our MapShape and it will be the single unit in our layer. class MapShape { private Feature feature; private ZoomLevelSet zoomLevelSet; public MapShape() : this(new Feature()) { } // Let's use this as a handy constructor if you already have // a feature or want to create one inline. public MapShape(Feature feature) { this.feature = feature; zoomLevelSet = new ZoomLevelSet(); } // This is the feature property, pretty simple. public Feature Feature { get { return feature; } set { feature = value; } } // This is the Zoom Level Set. This high level object has all of // the logic in it for zoom levels, drawing and everything. public ZoomLevelSet ZoomLevels { get { return zoomLevelSet; } set { zoomLevelSet = value; } } } } ====MapShapeLayer.cs==== using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using ThinkGeo.MapSuite.Core; namespace MapShapes { // Ideally I would want to make this inherit from the FeatureLayer // so you could do spatial queries. In the interests of time I // inherited from the Layer to make things simple and show the point // of how easy it is to extend Map Suite. When we roll this into // the main product we may create a FeatureSource and FeatureLayer. class MapShapeLayer : Layer { private Dictionary mapShapes; public MapShapeLayer() { mapShapes = new Dictionary(); } // Here is where you place all of your map shapes. public Dictionary MapShapes { get { return mapShapes; } } // This is a required overload of the Layer. As you can see we simply // loop through all of our map shapes and then choose the correct zoom level. // After that, the zoom level class takes care of the heavy lifiting. You // have to love how easy this framework is to re-use. protected override void DrawCore(GeoCanvas canvas, Collection labelsInAllLayers) { foreach (string mapShapeKey in mapShapes.Keys) { MapShape mapShape = mapShapes[mapShapeKey]; ZoomLevel currentZoomLevel = mapShape.ZoomLevels.GetZoomLevelForDrawing(canvas.CurrentWorldExtent, canvas.Width,GeographyUnit.DecimalDegree); if (currentZoomLevel != null) { if (canvas.CurrentWorldExtent.Intersects(mapShape.Feature.GetBoundingBox())) { currentZoomLevel.Draw(canvas, new Feature[] { mapShape.Feature }, new Collection(), labelsInAllLayers); } } } } } } ====Program.cs==== using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace MapShapes { 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 MapShapes { public partial class TestForm : Form { private MapEngine mapEngine = new MapEngine(); private Bitmap bitmap = null; private ShapeFileFeatureLayer worldLayer = null; public TestForm() { InitializeComponent(); } private void TestForm_Load(object sender, EventArgs e) { // Set the full extent and the background color mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-180.0, 83.0, 180.0, -90.0), Map.Width, Map.Height); mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean); // Add the worldLayer to the MapEngine worldLayer = new ShapeFileFeatureLayer(@"..\..\Data\Countries02.shp", ShapeFileReadWriteMode.ReadOnly); worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.County1; worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; mapEngine.StaticLayers.Add("WorldLayer", worldLayer); // This is just a code snippet to show you how the new // layer should be used. // Add the mapShapeLayer to the MapEngine MapShapeLayer mapShapeLayer = new MapShapeLayer(); MapShape mapShape1 = new MapShape(new Feature(-104, 42)); mapShape1.ZoomLevels.ZoomLevel01.DefaultPointStyle = new PointStyle(new GeoImage(@"..\..\Data\Cargo Plane.png")); mapShape1.ZoomLevels.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; mapShapeLayer.MapShapes.Add("1", mapShape1); MapShape mapShape2 = new MapShape(new Feature(104, 39)); mapShape2.ZoomLevels.ZoomLevel01.DefaultPointStyle = new PointStyle(new GeoImage(@"..\..\Data\China.png")); mapShape2.ZoomLevels.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; mapShapeLayer.MapShapes.Add("2", mapShape2); worldLayer.Open(); Feature feature3 = worldLayer.QueryTools.GetFeatureById("222", ReturningColumnsType.AllColumns); MapShape mapShape3 = new MapShape(feature3); mapShape3.ZoomLevels.ZoomLevel01.DefaultAreaStyle = AreaStyles.Forest1; mapShape3.ZoomLevels.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; mapShapeLayer.MapShapes.Add("3", mapShape3); BaseShape shape = new PointShape(-71, -52).GetShortestLineTo(new PointShape(38, 8), GeographyUnit.DecimalDegree); MapShape mapShape4 = new MapShape(new Feature(shape)); mapShape4.ZoomLevels.ZoomLevel01.DefaultLineStyle = LineStyles.ContestedBorder2; mapShape4.ZoomLevels.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; mapShapeLayer.MapShapes.Add("4", mapShape4); mapEngine.StaticLayers.Add("MapShapeLayer", mapShapeLayer); DrawImage(); } private void DrawImage() { if (bitmap != null) { bitmap.Dispose(); } bitmap = new Bitmap(Map.Width, Map.Height); mapEngine.OpenAllLayers(); mapEngine.DrawStaticLayers(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(); } } }