====== Source Code ServicesEditionSample LabelToShowLocation CS 091218.zip ======
====Program.cs====
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace LabelToShowLocation
{
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.Collections.ObjectModel;
using System.Drawing;
using System.Windows.Forms;
using ThinkGeo.MapSuite.Core;
namespace LabelToShowLocation
{
public partial class TestForm : Form
{
private MapEngine mapEngine = new MapEngine();
private Bitmap bitmap = null;
public TestForm()
{
InitializeComponent();
}
private void TestForm_Load(object sender, EventArgs e)
{
// Set the extent and the background color
mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-83.2794,36.8835,-82.755,36.5744), 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);
//Layers for labeling location only, no displaying. Using counties and states.
ShapeFileFeatureLayer Layer1 = new ShapeFileFeatureLayer(@"..\..\Data\counties.shp");
ShapeFileFeatureLayer Layer2 = new ShapeFileFeatureLayer(@"..\..\Data\USStates.shp");
mapEngine.StaticLayers.Add("CountyLayer", Layer1);
mapEngine.StaticLayers.Add("StateLayer", Layer2);
DrawImage();
LabelLocation();
}
//Function for updating the label with the location of the center of the map with the county and state information.
private void LabelLocation()
{
string county = "";
string state = "";
//Gets the point in world coordinate of the center of the current extent.
PointShape centerPointShape = ExtentHelper.ToWorldCoordinate(mapEngine.CurrentExtent, bitmap.Width / 2,
bitmap.Height / 2, bitmap.Width, bitmap.Height);
//Displays the label for county only if current extent is within 1 degree (if using decimal degrees as GeographyUnit)
if (mapEngine.CurrentExtent.Width < 1)
{
ShapeFileFeatureLayer countyLayer = (ShapeFileFeatureLayer)mapEngine.StaticLayers["CountyLayer"];
countyLayer.Open();
//Spatial query to find the county being in the center of the map.
Collection countyFeatures = countyLayer.FeatureSource.SpatialQuery(centerPointShape, QueryType.Contains, ReturningColumnsType.AllColumns);
countyLayer.Close();
if (countyFeatures.Count > 0)
{
county = countyFeatures[0].ColumnValues["Name"] + " county, ";
}
}
//Displays the label for the state only if current extent is withing 5 degrees (if using deciaml degrees as GeographyUnit)
if (mapEngine.CurrentExtent.Width < 5)
{
ShapeFileFeatureLayer stateLayer = (ShapeFileFeatureLayer)mapEngine.StaticLayers["StateLayer"];
stateLayer.Open();
//Spatial query to find the state being in the center of the map.
Collection stateFeatures = stateLayer.QueryTools.GetFeaturesContaining(centerPointShape, ReturningColumnsType.AllColumns);
stateLayer.Close();
if (stateFeatures.Count > 0)
{
state = stateFeatures[0].ColumnValues["State_name"];
}
}
lblLocation.Text = county + state;
}
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);
LabelLocation();
break;
case "Zoom Out":
mapEngine.CurrentExtent.ScaleUp(50);
LabelLocation();
break;
case "Full Extent":
mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-180.0, 83.0, 180.0, -90.0), Map.Width, Map.Height);
lblLocation.Text = "";
break;
case "Pan Left":
mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Left, 20);
LabelLocation();
break;
case "Pan Right":
mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Right, 20);
LabelLocation();
break;
case "Pan Up":
mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Up, 20);
LabelLocation();
break;
case "Pan Down":
mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Down, 20);
LabelLocation();
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);
}
}
}