Table of Contents

Source Code GeocoderSampleService AddressLocator CS.zip

Settings.Designer.cs

 //------------------------------------------------------------------------------  
 // <auto-generated>  
 //     This code was generated by a tool.  
 //     Runtime Version:4.0.30319.18444  
 //  
 //     Changes to this file may cause incorrect behavior and will be lost if  
 //     the code is regenerated.  
 // </auto-generated>  
 //------------------------------------------------------------------------------  
 
 namespace ThinkGeo.MapSuite.GeoCoderExamples.Properties {  
 
 
     [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]  
     [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator",|"10.0.0.0")]  
     internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {  
 
         private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));  
 
         public static Settings Default {  
             get {  
                 return defaultInstance;  
             }  
         }  
     }  
 }  
 

GeocoderSearchType.cs

 namespace ThinkGeo.MapSuite.GeoCoderExamples  
 {  
     public enum GeocoderSearchType  
     {  
         Street = 0,  
         Reverse = 1  
     }  
 }  
 

MapSuiteSampleHelper.cs

 using System.Collections.Generic;  
 
 namespace ThinkGeo.MapSuite.GeoCoderExamples  
 {  
     public static class MapSuiteSampleHelper  
     {  
         public static IEnumerable<string> GetGeocoderAddressCandidates(GeocoderSearchType searchType)  
         {  
             switch (searchType)  
             {  
                 case GeocoderSearchType.Reverse:  
                     yield return "42.020431 -87.666757";  
                     yield return "42.017069 -87.672102";  
                     yield return "42.016106 -87.668558";  
                     yield return "42.005451 -87.664937";  
                     yield return "42.011431 -87.678457";  
                     yield return "42.013912 -87.699847";  
                     break;  
 
                 default:  
                     yield return "5300 N Winthrop Ave";  
                     yield return "1401 W Ainslie St";  
                     yield return "1401 W Estes Ave";  
                     yield return "7430 N Seeley Ave";  
                     yield return "7400 N Greenview Ave";  
                     yield return "1101 W Farwell Ave";  
                     break;  
             }  
         }  
     }  
 }  
 

TrackMode.cs

 using System;  
 
 namespace ThinkGeo.MapSuite.GeoCoderExamples  
 {  
     [Serializable]  
     public enum TrackMode  
     {  
         None = 0,  
         Point = 1,  
         Line = 2,  
         Polygon = 3,  
         Rectangle = 4,  
         Square = 5,  
         Circle = 6,  
         Ellipse = 7,  
         StraightLine = 8,  
         Freehand = 9,  
         Custom = 10,  
     }  
 }  
 

CollapsiblePanel.cs

 using System;  
 using System.Drawing;  
 using System.Drawing.Drawing2D;  
 using System.Linq;  
 using System.Windows.Forms;  
 
 namespace ThinkGeo.MapSuite.GeoCoderExamples  
 {  
     public class CollapsiblePanel : Panel  
     {  
         private int lineWidth;  
         private int panelWidth;  
         private bool isCollapsible;  
         private Size collapsibleBoxSize;  
         private PictureBox picCollapsibleBox;  
 
         public event EventHandler PanelCollapseButtonClick;  
 
         public CollapsiblePanel()  
         {  
             lineWidth = 5;  
             isCollapsible = false;  
             collapsibleBoxSize = new Size(12, 110);  
 
             picCollapsibleBox = new PictureBox();  
             picCollapsibleBox.BackColor = Color.Transparent;  
             picCollapsibleBox.Location = new Point(Width - lineWidth - collapsibleBoxSize.Width, Height / 2 - collapsibleBoxSize.Height / 2);  
             picCollapsibleBox.Size = collapsibleBoxSize;  
             picCollapsibleBox.Click += CollapsiblePictureBox_Click;  
             picCollapsibleBox.MouseEnter += CollapsiblePictureBox_MouseEnter;  
             picCollapsibleBox.MouseLeave += CollapsiblePictureBox_MouseLeave;  
             picCollapsibleBox.Image = GetCollapsibleImage();  
             Controls.Add(picCollapsibleBox);  
 
             Width = lineWidth + panelWidth + picCollapsibleBox.Width;  
             Resize += CollapsiblePanel_Resize;  
         }  
 
         public int LineWidth  
         {  
             get { return lineWidth; }  
             set { lineWidth = value; }  
         }  
 
         public int PanelWidth  
         {  
             get { return panelWidth; }  
             set { panelWidth = value; }  
         }  
 
         protected override void OnPaint(PaintEventArgs e)  
         {  
             base.OnPaint(e);  
 
             Rectangle drawingRectangle = new Rectangle(Width - lineWidth, 0, lineWidth, Height);  
             LinearGradientBrush myBrush = new LinearGradientBrush(drawingRectangle, Color.Gray, Color.White, LinearGradientMode.Horizontal);  
             e.Graphics.FillRectangle(myBrush, drawingRectangle);  
         }  
 
         protected void OnPanelCollapseButtonClick(EventArgs e)  
         {  
             EventHandler handler = PanelCollapseButtonClick;  
             if (handler != null) handler(this, e);  
         }  
 
         private void CollapsiblePanel_Resize(object sender, EventArgs e)  
         {  
             picCollapsibleBox.Location = new Point(Width - lineWidth - collapsibleBoxSize.Width, Height / 2 - collapsibleBoxSize.Height / 2);  
         }  
 
         private Bitmap GetCollapsibleImage()  
         {  
             if (picCollapsibleBox.Image != null) picCollapsibleBox.Image.Dispose();  
 
             Bitmap bitmap = new Bitmap(picCollapsibleBox.Size.Width, picCollapsibleBox.Size.Height);  
             using (Graphics g = Graphics.FromImage(bitmap))  
             {  
                 Size imageSize = Properties.Resources.collapse.Size;  
                 int x = (picCollapsibleBox.Width - imageSize.Width) / 2;  
                 int y = (picCollapsibleBox.Height - imageSize.Height) / 2;  
                 g.DrawImage(isCollapsible ? Properties.Resources.expand : Properties.Resources.collapse, x, y);  
             }  
 
             return bitmap;  
         }  
 
         private void CollapsiblePictureBox_Click(object sender, EventArgs e)  
         {  
             isCollapsible = !isCollapsible;  
             picCollapsibleBox.Image = GetCollapsibleImage();  
 
             foreach (var item in Controls.OfType<Control>().Where(c => c != picCollapsibleBox))  
             {  
                 item.Visible = !isCollapsible;  
             }  
 
             Width = isCollapsible ? lineWidth + collapsibleBoxSize.Width : panelWidth + lineWidth + collapsibleBoxSize.Width;  
             Refresh();  
 
             OnPanelCollapseButtonClick(e);  
         }  
 
         private void CollapsiblePictureBox_MouseEnter(object sender, EventArgs e)  
         {  
             picCollapsibleBox.BackColor = Color.FromArgb(150, 4, 60, 153);  
         }  
 
         private void CollapsiblePictureBox_MouseLeave(object sender, EventArgs e)  
         {  
             picCollapsibleBox.BackColor = Color.Transparent;  
         }  
     }  
 }  
 

MainForm.cs

 using System;  
 using System.Collections.Generic;  
 using System.Collections.ObjectModel;  
 using System.Data;  
 using System.Drawing;  
 using System.Drawing.Drawing2D;  
 using System.Drawing.Imaging;  
 using System.IO;  
 using System.Threading.Tasks;  
 using System.Windows.Forms;  
 using ThinkGeo.MapSuite.Core;  
 using ThinkGeo.MapSuite.GeoCoderExamples.Properties;  
 using ThinkGeo.MapSuite.MapSuiteGeocoder;  
 
 namespace ThinkGeo.MapSuite.GeoCoderExamples  
 {  
     public partial class MainForm : Form  
     {  
         private RectangleShape defaultExtent = new RectangleShape(-88.3330001640625, 42.5966329101563, -86.9157638359375, 41.1629170898438);  
 
         private InMemoryFeatureLayer markersLayer;  
         private MapEngine mapEngine;  
         private Bitmap bitmap;  
         private GeographyUnit mapUnit;  
 
         public MainForm()  
         {  
             InitializeComponent();  
         }  
 
         private GeocoderSearchType SearchType  
         {  
             get { return cmbSearchType.SelectedIndex == 0 ? GeocoderSearchType.Street : GeocoderSearchType.Reverse; }  
             set  
             {  
                 cmbSearchType.SelectedIndex = (int)value;  
                 cmbAddress.Items.Clear();  
                 foreach (var item in MapSuiteSampleHelper.GetGeocoderAddressCandidates(value))  
                 {  
                     cmbAddress.Items.Add(item);  
                 }  
 
                 cmbAddress.SelectedItem = cmbAddress.Items[0];  
                 switch (value)  
                 {  
                     case GeocoderSearchType.Reverse:  
                         lblEnter.Text = Resources.MainForm_SearchType_Enter_Coordinates_;  
                         lblExample.Text = Resources.MainForm_SearchType__ex___42_011431__87_678457__etc__;  
                         break;  
                     case GeocoderSearchType.Street:  
                         lblEnter.Text = Resources.MainForm_SearchType_Enter_an_Address_;  
                         lblExample.Text = Resources.MainForm_SearchType__ex___5300_N_Winthrop_Ave__etc__;  
                         break;  
                 }  
             }  
         }  
 
         private void MainForm_Load(object sender, EventArgs e)  
         {  
             mapEngine = new MapEngine();  
             bitmap = new Bitmap(mapPicture.Width, mapPicture.Height);  
             SearchType = GeocoderSearchType.Street;  
 
             mapUnit = GeographyUnit.DecimalDegree;  
             mapEngine.CurrentExtent = defaultExtent;  
             mapPicture.MouseMove += Map_MouseMove;  
 
             markersLayer = new InMemoryFeatureLayer();  
             markersLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = new PointStyle(GetMarkerImage());  
             markersLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
 
             mapEngine.StaticLayers.Add("WMK", new WorldMapKitLayer());  
             mapEngine.StaticLayers.Add(markersLayer);  
 
             leftSideBarPanel.PanelCollapseButtonClick += LeftSideBarPanel_PanelCollapseButtonClick;  
             SearchAddress(cmbAddress.Text);  
         }  
 
         private void SearchButton_Click(object sender, EventArgs e)  
         {  
             SearchAddress(cmbAddress.Text);  
         }  
 
         private void ClearButton_Click(object sender, EventArgs e)  
         {  
             dgvQueryResultItems.DataSource = null;  
             dgvQueryResultItems.Rows.Clear();  
             markersLayer.InternalFeatures.Clear();  
 
             RefreshMap();  
         }  
 
         public void RefreshMap()  
         {  
             if (mapEngine.StaticLayers.Count > 0 && mapPicture.Width > 0)  
             {  
                 bitmap = new Bitmap(mapPicture.Width, mapPicture.Height);  
                 mapEngine.OpenAllLayers();  
                 mapPicture.CreateGraphics().CompositingMode = CompositingMode.SourceCopy;  
                 mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(mapEngine.CurrentExtent, mapPicture.Width, mapPicture.Height);  
                 mapEngine.DrawStaticLayers(bitmap, mapUnit);  
                 mapEngine.DrawDynamicLayers(bitmap, mapUnit);  
                 mapEngine.DrawAdornmentLayers(bitmap, mapUnit);  
                 mapEngine.CloseAllLayers();  
                 UpdateImageSource();  
                 mapPicture.Refresh();  
             }  
         }  
 
         private void UpdateImageSource()  
         {  
             Image oldImage = mapPicture.Image;  
             mapPicture.Image = bitmap;  
             if (oldImage != null) oldImage.Dispose();  
         }  
 
         private void Map_MouseMove(object sender, MouseEventArgs e)  
         {  
             PointShape mouseLocation = ExtentHelper.ToWorldCoordinate(mapEngine.CurrentExtent, new ScreenPointF(e.X, e.Y), mapPicture.Width, mapPicture.Height);  
             lblFooterLocationX.Text = string.Format("X:{0:0.######}", mouseLocation.X);  
             lblFooterLocationY.Text = string.Format("Y:{0:0.######}", mouseLocation.Y);  
         }  
 
         private void QueryResultItemsDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)  
         {  
             DataGridView dataGrid = sender as DataGridView;  
             if (e.ColumnIndex == 0 && e.RowIndex >= 0 && dataGrid != null)  
             {  
                 DataGridViewImageCell imageCell = dataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewImageCell;  
                 if (imageCell != null) ZoomToShape((PointShape)imageCell.Tag);  
             }  
         }  
 
         private void QueryResultItemsDataGridView_CellMouseEnter(object sender, DataGridViewCellEventArgs e)  
         {  
             if (e.ColumnIndex == 0 && e.RowIndex >= 0)  
             {  
                 DataGridViewCell cell = dgvQueryResultItems.Rows[e.RowIndex].Cells[e.ColumnIndex];  
                 if (cell.Value != null)  
                 {  
                     cell.Style.BackColor = Color.LightBlue;  
                 }  
             }  
         }  
 
         private void QueryResultItemsDataGridView_CellMouseLeave(object sender, DataGridViewCellEventArgs e)  
         {  
             if (e.ColumnIndex == 0 && e.RowIndex >= 0)  
             {  
                 DataGridViewCell cell = dgvQueryResultItems.Rows[e.RowIndex].Cells[e.ColumnIndex];  
                 if (cell.Value != null)  
                 {  
                     cell.Style.BackColor = Color.White;  
                 }  
             }  
         }  
 
         private void SearchTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)  
         {  
             SearchType = cmbSearchType.SelectedIndex == 0 ? GeocoderSearchType.Street : GeocoderSearchType.Reverse;  
         }  
 
         private void LeftSideBarPanel_PanelCollapseButtonClick(object sender, EventArgs e)  
         {  
             mainContainer.Width = Width - leftSideBarPanel.Width;  
             mainContainer.Left = leftSideBarPanel.Width;  
             RefreshMap();  
         }  
 
         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(defaultExtent, bitmap.Width, bitmap.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;  
             }  
 
             RefreshMap();  
         }  
 
         private void SearchAddress(string text)  
         {  
             Collection<GeocoderMatch> results;  
             Collection<GeocoderMatch> searchResultItems = new Collection<GeocoderMatch>();  
             string matchResultFilter = SearchType == GeocoderSearchType.Street ? "Street" : string.Empty;  
             UsaGeocoder usaGeocoder = new UsaGeocoder(@"App_Data\", MatchMode.ExactMatch,  
                 StreetNumberMatchingMode.Default);  
             try  
             {  
                 usaGeocoder.Open();  
                 results = usaGeocoder.Match(text);  
             }  
             finally  
             {  
                 usaGeocoder.Close();  
             }  
 
             markersLayer.InternalFeatures.Clear();  
             dgvQueryResultItems.DataSource = null;  
 
             MultipointShape multiPointShape = new MultipointShape();  
             foreach (GeocoderMatch item in results)  
             {  
                 if (string.IsNullOrEmpty(matchResultFilter) || item.NameValuePairs.ContainsKey(matchResultFilter))  
                 {  
                     PointShape point = new PointShape(item.NameValuePairs["CentroidPoint"]);  
                     multiPointShape.Points.Add(point);  
                     markersLayer.InternalFeatures.Add(new Feature(point));  
 
                     searchResultItems.Add(item);  
                 }  
             }  
 
             UpdateResultListView(searchResultItems);  
             if (multiPointShape.Points.Count > 0)  
             {  
                 ZoomToShape(multiPointShape);  
             }  
         }  
 
         private void ZoomToShape(BaseShape pointShape)  
         {  
             RectangleShape rect = pointShape.Buffer(100, GeographyUnit.DecimalDegree, DistanceUnit.Meter).GetBoundingBox();  
             mapEngine.CurrentExtent = rect;  
             RefreshMap();  
         }  
 
         private void UpdateResultListView(IList<GeocoderMatch> searchResultItems)  
         {  
             DataTable resultTable = new DataTable();  
             if (searchResultItems.Count > 0)  
             {  
                 dgvQueryResultItems.Columns[0].Visible = true;  
                 foreach (var nameValuePair in searchResultItems[0].NameValuePairs)  
                 {  
                     resultTable.Columns.Add(new DataColumn(nameValuePair.Key));  
                 }  
 
                 foreach (var searchResultItem in searchResultItems)  
                 {  
                     DataRow row = resultTable.NewRow();  
                     foreach (var nameValuePair in searchResultItem.NameValuePairs)  
                     {  
                         row[nameValuePair.Key] = nameValuePair.Value;  
                     }  
                     resultTable.Rows.Add(row);  
                 }  
                 dgvQueryResultItems.ColumnHeadersVisible = true;  
             }  
             else  
             {  
                 dgvQueryResultItems.Columns[0].Visible = false;  
                 DataColumn column = new DataColumn(" ");  
                 resultTable.Columns.Add(column);  
                 DataRow row = resultTable.NewRow();  
                 row[column] = "No match found";  
                 resultTable.Rows.Add(row);  
                 dgvQueryResultItems.ColumnHeadersVisible = false;  
             }  
 
             dgvQueryResultItems.DataSource = resultTable;  
             foreach (DataGridViewRow row in dgvQueryResultItems.Rows)  
             {  
                 DataGridViewImageCell zoomToImageCell = new DataGridViewImageCell();  
                 zoomToImageCell.Style.SelectionBackColor = Color.Transparent;  
                 zoomToImageCell.Value = Icon.FromHandle(Resources.find.GetHicon());  
                 try  
                 {  
                     zoomToImageCell.Tag = BaseShape.CreateShapeFromWellKnownData(row.Cells["CentroidPoint"].Value as string);  
                 }  
                 catch (Exception)  
                 { }  
                 row.Cells[0] = zoomToImageCell;  
                 row.Cells[0].Selected = false;  
             }  
         }  
 
         private static GeoImage GetMarkerImage()  
         {  
             MemoryStream msStream = new MemoryStream();  
             Resources.marker.Save(msStream, ImageFormat.Png);  
             GeoImage markerImage = new GeoImage(msStream);  
             return markerImage;  
         }  
     }  
 }  
 

Program.cs

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Windows.Forms;  
 
 namespace ThinkGeo.MapSuite.GeoCoderExamples  
 {  
     static class Program  
     {  
         /// <summary>  
         /// The main entry point for the application.  
         /// </summary>  
         [STAThread]  
         static void Main()  
         {  
             Application.EnableVisualStyles();  
             Application.SetCompatibleTextRenderingDefault(false);  
             Application.Run(new MainForm());  
         }  
     }  
 }