====== Source Code GeocoderSampleWeb AddressLocator CS.zip ======
====Default.aspx.cs====
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.MapSuiteGeocoder;
using ThinkGeo.MapSuite.WebEdition;
namespace ThinkGeo.MapSuite.GeoCoderExamples
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Initialize the map.
Map1.MapUnit = GeographyUnit.DecimalDegree;
Map1.CustomOverlays.Add(new WorldMapKitWmsWebOverlay("WMKOverlay"));
Map1.CustomOverlays.Add(new SimpleMarkerOverlay("MarkerOverlay"));
Map1.CurrentExtent = new RectangleShape(-88.3330001640625, 42.5966329101563, -86.9157638359375, 41.1629170898438);
// Find result by default address.
Collection matchedItems = SearchAddress(ddlGeoCoderDemoItems.SelectedValue);
FillMarkers(matchedItems);
FillResultTable(matchedItems);
}
}
protected void BtnSearch_Click(object sender, EventArgs e)
{
Collection matchedItems = new Collection();
if (ddlSearchType.SelectedIndex == 0)
{
matchedItems = SearchAddress(ddlGeoCoderDemoItems.SelectedValue);
}
else
{
matchedItems = SearchAddress(ddlReverseGeoCoderDemoItems.SelectedValue);
}
FillMarkers(matchedItems);
FillResultTable(matchedItems);
}
protected void BtnClear_Click(object sender, EventArgs e)
{
GridView1.DataSource = null;
GridView1.DataBind();
SimpleMarkerOverlay simpleMarkerOverlay = (SimpleMarkerOverlay)Map1.CustomOverlays["MarkerOverlay"];
simpleMarkerOverlay.Markers.Clear();
}
protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Find")
{
ImageButton imageButton = (ImageButton)e.CommandSource;
TableCellCollection cells = ((GridViewRow)imageButton.Parent.Parent).Cells;
string shapeWkt = cells[cells.Count|- 1].Text;
if (!string.IsNullOrEmpty(shapeWkt))
{
BaseShape targetShape = BaseShape.CreateShapeFromWellKnownData(shapeWkt);
PointShape targetWorldCenterPoint = targetShape as PointShape;
if (targetWorldCenterPoint != null && targetWorldCenterPoint.X != 0)
{
Map1.ZoomTo(targetWorldCenterPoint, new ZoomLevelSet().ZoomLevel20.Scale);
}
}
}
}
private Collection SearchAddress(string text)
{
Collection results;
UsaGeocoder usaGeocoder = new UsaGeocoder(MapPath("~/App_Data/"), MatchMode.ExactMatch);
try
{
usaGeocoder.Open();
results = usaGeocoder.Match(text);
}
finally
{
usaGeocoder.Close();
}
Collection searchResultItems = new Collection();
string searchType = ddlSearchType.SelectedIndex == 0 ? "Street" : string.Empty;
foreach (GeocoderMatch item in results)
{
if (string.IsNullOrEmpty(searchType) ||
item.NameValuePairs.ContainsKey(searchType))
{
// Add address marker to map.
searchResultItems.Add(item);
}
}
return searchResultItems;
}
private void FillMarkers(IEnumerable matches)
{
MultipointShape resultPoints = new MultipointShape();
SimpleMarkerOverlay simpleMarkerOverlay = (SimpleMarkerOverlay)Map1.CustomOverlays["MarkerOverlay"];
simpleMarkerOverlay.Markers.Clear();
foreach (GeocoderMatch matchItem in matches)
{
PointShape point = new PointShape(matchItem.NameValuePairs["CentroidPoint"]);
resultPoints.Points.Add(point);
// Get the popup context.
string popupContext = GetPopupContent(matchItem);
CustomPopup popup = new CustomPopup("", point, popupContext);
popup.BorderWidth = 1;
popup.IsVisible = false;
popup.AutoSize = true;
Marker marker = new Marker(point) { WebImage = new WebImage("/Images/marker.png") };
marker.Popup = popup;
simpleMarkerOverlay.Markers.Add(marker);
}
if (resultPoints.Points.Count == 1)
{
Map1.ZoomTo(resultPoints.Points[0], new ZoomLevelSet().ZoomLevel20.Scale);
}
else if (resultPoints.Points.Count > 1)
{
Map1.CurrentExtent = resultPoints.GetBoundingBox();
}
}
private void FillResultTable(IList searchResultItems)
{
DataTable resultTable = new DataTable();
if (searchResultItems.Count > 0)
{
foreach (var nameValuePair in searchResultItems[0].NameValuePairs)
{
DataColumn column = new DataColumn { ColumnName = nameValuePair.Key };
resultTable.Columns.Add(column);
}
foreach (var searchResultItem in searchResultItems)
{
DataRow row = resultTable.NewRow();
foreach (var nameValuePair in searchResultItem.NameValuePairs)
{
row[nameValuePair.Key] = nameValuePair.Value;
}
resultTable.Rows.Add(row);
}
}
GridView1.DataSource = resultTable;
GridView1.DataBind();
}
private static string GetPopupContent(GeocoderMatch matchItem)
{
string[] streetkeyList = { "Street", "Zip", "State" };
StringBuilder keyValuesBuilder = new StringBuilder();
foreach (string item in streetkeyList)
{
keyValuesBuilder.Append(string.Format(CultureInfo.InvariantCulture, "{0} ", matchItem.NameValuePairs[item]));
}
string headerText = keyValuesBuilder.ToString();
StringBuilder popupHtml = new StringBuilder("");
popupHtml.Append(string.Format(CultureInfo.InvariantCulture, "
", headerText));
popupHtml.Append(" |
");
foreach (var pair in matchItem.NameValuePairs.Take(5))
{
popupHtml.Append(string.Format("", pair.Key, pair.Value));
}
popupHtml.Append("
");
return popupHtml.ToString();
}
}
}
====Default.aspx====
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ThinkGeo.MapSuite.GeoCoderExamples.Default" %>
<%@ Register Assembly="WebEdition" Namespace="ThinkGeo.MapSuite.WebEdition" TagPrefix="cc1" %>
Chicago Address Locator