User Tools

Site Tools


source_code_geocodersampledesktop_addresslocator_cs.zip

Source Code GeocoderSampleDesktop AddressLocator CS.zip

MainForm.cs

 using System;  
 using System.Collections.Generic;  
 using System.Collections.ObjectModel;  
 using System.Data;  
 using System.Drawing;  
 using System.Globalization;  
 using System.Linq;  
 using System.Text;  
 using System.Windows.Forms;  
 using ThinkGeo.MapSuite.Core;  
 using ThinkGeo.MapSuite.DesktopEdition;  
 using ThinkGeo.MapSuite.GeoCoderExamples.Properties;  
 using ThinkGeo.MapSuite.MapSuiteGeocoder;  
 
 namespace ThinkGeo.MapSuite.GeoCoderExamples  
 {  
     public partial class MainForm : Form  
     {  
         private SimpleMarkerOverlay simpleMarkerOverlay;  
 
         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)  
         {  
             SearchType = GeocoderSearchType.Street;  
 
             map.MapUnit = GeographyUnit.DecimalDegree;  
             map.CurrentExtent = new RectangleShape(-88.3330001640625, 42.5966329101563, -86.9157638359375, 41.1629170898438);  
             map.MouseMove += Map_MouseMove;  
 
             simpleMarkerOverlay = new SimpleMarkerOverlay();  
             simpleMarkerOverlay.MapControl = map;  
 
             map.Overlays.Add("WMK", new WorldMapKitWmsDesktopOverlay());  
             map.Overlays.Add("SimpleMarker", simpleMarkerOverlay);  
 
             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;  
             simpleMarkerOverlay.Markers.Clear();  
             map.Controls.Clear();  
 
             map.Refresh();  
         }  
 
         private void Map_MouseMove(object sender, MouseEventArgs e)  
         {  
             PointShape mouseLocation = ExtentHelper.ToWorldCoordinate(map.CurrentExtent, new ScreenPointF(e.X, e.Y), map.Width, map.Height);  
             lblFooterLocationX.Text = string.Format(CultureInfo.InvariantCulture, "X:{0:0.######}", mouseLocation.X);  
             lblFooterLocationY.Text = string.Format(CultureInfo.InvariantCulture, "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 = (DataGridViewImageCell)dataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex];  
                 map.CurrentExtent = ((BaseShape)imageCell.Tag).GetBoundingBox();  
                 map.Refresh();  
             }  
         }  
 
         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)  
         {  
             mapContainer.Width = Width - leftSideBarPanel.Width;  
             mapContainer.Left = leftSideBarPanel.Width;  
         }  
 
         private void SearchAddress(string text)  
         {  
             Collection<GeocoderMatch> results;  
             UsaGeocoder usaGeocoder = new UsaGeocoder(@"App_Data\", MatchMode.ExactMatch, StreetNumberMatchingMode.Default);  
 
             try  
             {  
                 usaGeocoder.Open();  
                 results = usaGeocoder.Match(text);  
             }  
             finally  
             {  
                 usaGeocoder.Close();  
             }  
 
             simpleMarkerOverlay.Markers.Clear();  
             map.Controls.Clear();  
             dgvQueryResultItems.DataSource = null;  
 
             MultipointShape multiPointShape = new MultipointShape();  
             string matchString = SearchType == GeocoderSearchType.Street ? "Street" : string.Empty;  
             Collection<GeocoderMatch> searchResultItems = new Collection<GeocoderMatch>();  
             foreach (GeocoderMatch item in results)  
             {  
                 if (string.IsNullOrEmpty(matchString) || item.NameValuePairs.ContainsKey(matchString))  
                 {  
                     AddSearchResultMarkerWithToolTip(item, multiPointShape);  
                     searchResultItems.Add(item);  
                 }  
             }  
 
             UpdateResultListView(searchResultItems);  
             if (multiPointShape.Points.Count > 0) map.CurrentExtent = multiPointShape.GetBoundingBox();  
 
             map.Refresh();  
         }  
 
         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 ex)  
                 { }  
                 row.Cells[0] = zoomToImageCell;  
                 row.Cells[0].Selected = false;  
             }  
         }  
 
         private void AddSearchResultMarkerWithToolTip(GeocoderMatch matchItem, MultipointShape multiPointShape)  
         {  
             PointShape point = new PointShape(matchItem.NameValuePairs["CentroidPoint"]);  
             Marker marker = new Marker(point);  
             marker.Image = Resources.marker;  
             marker.ToolTipText = GetToolTip(matchItem);  
 
             multiPointShape.Points.Add(point);  
             simpleMarkerOverlay.Markers.Add(marker);  
         }  
 
         private static string GetToolTip(GeocoderMatch matchItem)  
         {  
             StringBuilder description = new StringBuilder();  
             description.AppendLine(GetHearTextFromMatch(matchItem));  
             foreach (var item in matchItem.NameValuePairs.Take(5))  
             {  
                 description.AppendLine(string.Format(CultureInfo.InvariantCulture, "{0} : {1}", item.Key, item.Value));  
             }  
 
             return description.ToString();  
         }  
 
         private static string GetHearTextFromMatch(GeocoderMatch matchItem)  
         {  
             StringBuilder keyValuesBuilder = new StringBuilder();  
 
             string[] keyList = { "Street", "Zip", "State" };  
             foreach (string item in keyList)  
             {  
                 keyValuesBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0} ", matchItem.NameValuePairs[item]);  
             }  
 
             return keyValuesBuilder.ToString();  
         }  
 
         private void label1_Click(object sender, EventArgs e)  
         {  
 
         }  
     }  
 }  
 

Program.cs

 using System;  
 using System.Collections.Generic;  
 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());  
         }  
     }  
 }  
 

CollapsiblePanel.cs

 using System;  
 using System.Drawing;  
 using System.Drawing.Drawing2D;  
 using System.Linq;  
 using System.Windows.Forms;  
 using ThinkGeo.MapSuite.GeoCoderExamples.Properties;  
 
 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 = Resources.collapse.Size;  
                 int x = (picCollapsibleBox.Width - imageSize.Width) / 2;  
                 int y = (picCollapsibleBox.Height - imageSize.Height) / 2;  
                 g.DrawImage(isCollapsible ? Resources.expand : 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;  
         }  
     }  
 }  
 

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;  
             }  
         }  
     }  
 }  
 

GeocoderSearchType.cs

 namespace ThinkGeo.MapSuite.GeoCoderExamples  
 {  
     public enum GeocoderSearchType  
     {  
         Street = 0,  
         Reverse = 1  
     }  
 }  
 
 
source_code_geocodersampledesktop_addresslocator_cs.zip.txt · Last modified: 2015/09/09 03:33 by admin