ArcIMS Geocoding
ArcIMS_Geocoding_CSharp\Default.aspx.cs
// Copyright 2011 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
// 



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ESRI.ArcGIS.ADF.IMS.Carto;
using ESRI.ArcGIS.ADF.Connection.IMS;
using ESRI.ArcGIS.ADF.Web;
using ESRI.ArcGIS.ADF.IMS.Carto.Layer;
using ESRI.ArcGIS.ADF.IMS.Geocode;
using System.Collections;
using ESRI.ArcGIS.ADF.Web.Geometry;
using ESRI.ArcGIS.ADF.IMS.Display.AcetateElement;
using ESRI.ArcGIS.ADF.IMS.Display.Symbol;

public partial class _Default : System.Web.UI.Page
{

    private string appservername = "servername";
    private int appserverport = 5300;
    private string servicename = "SanFrancisco";
    private string geocodelayername = "streets";
    
    private TCPConnection serverconnection;
    private MapService mapservice;
    private MapView mapview;
    private LayerCollection layercollection;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            if (Session.IsNewSession)            {                
                
                serverconnection = new TCPConnection(appservername, appserverport);
                serverconnection.ServiceName = servicename;
                mapservice = new MapService(serverconnection, new InitializationParameters(), true);
                mapview = mapservice.CreateMapView();
                ESRI.ArcGIS.ADF.IMS.Carto.ImageDescriptor imgdesc = mapview.ImageDescriptor;

                imgdesc.Height = (int) ((Image1.Height).Value);
                imgdesc.Width = (int) ((Image1.Width).Value);

                layercollection = mapview.Layers;
                                
                this.DrawMap();
                this.DrawLegend();

                Session.Add("sc", serverconnection);
                Session.Add("ms", mapservice);
                Session.Add("mv", mapview);
                             
            }
        } else
        {
            serverconnection = Session["sc"] as TCPConnection;
            mapservice = Session["ms"] as MapService;
            mapview = Session["mv"] as MapView;
            layercollection = mapview.Layers;
        }
    }

    private void DrawMap()
    {
        mapview.Draw();
        CartoImage cartoimage = mapview.Image;
        this.Image1.ImageUrl = cartoimage.Url;
    }

    private void DrawLegend()
    {
        Legend legend = mapview.Legend;
        legend.Height = (int)((Image2.Height).Value);
        legend.Width = (int)((Image2.Width).Value);
        legend.Draw();
        CartoImage legendimage = legend.Image;
        this.Image2.ImageUrl = legendimage.Url;

    }

    private void ShowDebug(String message)
    {
        System.Diagnostics.Debug.WriteLine(message);

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
        Layer layer = layercollection.FindByName(geocodelayername);
        FeatureLayer flayer = (FeatureLayer)layer;

        Geocoder geocoder = flayer.Geocoder;
        geocoder.MaxCandidates = Int32.Parse(DropDownList2.Text);
        geocoder.MinScore = Int32.Parse(DropDownList1.Text);
        
        ShowDebug(geocoder.Style);
        InputFieldCollection ifc = geocoder.InputFields;
        IEnumerator ie = ifc.GetEnumerator();
        ie.Reset(); 
        while (ie.MoveNext())
        {
          InputField inf = (InputField) ie.Current;
          ShowDebug(inf.Description);
          ShowDebug(inf.ID);
          ShowDebug(inf.Label);
        }

        AddressValueCollection avc = new AddressValueCollection();
        AddressValue av1 = new AddressValue("STREET", TextBox1.Text);
        AddressValue av2 = new AddressValue("ZONE", TextBox3.Text);
        avc.Add(av1);
        avc.Add(av2);
        FeatureTable ft = geocoder.Geocode(avc);

        AcetateLayer alayer = (AcetateLayer)layercollection.FindByName("mygeocodepoints");

        if (ft.Rows.Count > 0)
        {
            if (alayer != null)
            {
                alayer.AcetateElements.Clear();
            }
            else
            {
                alayer = this.CreateAcetateLayer("mygeocodepoints");
            }

            IEnumerator ftrows = ft.Rows.GetEnumerator();
            while (ftrows.MoveNext())
            {
                DataRow dr = (DataRow)ftrows.Current;
                int ss = (int) dr["SCORE"];
                string af = (string) dr["ADDRESSFOUND"];
                ESRI.ArcGIS.ADF.IMS.Geometry.Point pnt = (ESRI.ArcGIS.ADF.IMS.Geometry.Point)dr["#SHAPE#"];
                this.AddGeometry(pnt, alayer);
            }
        }
        else if (alayer != null)
        {
            alayer.AcetateElements.Clear();
            layercollection.Remove(alayer);
        }
        
        IEnumerator ftcols = ft.Columns.GetEnumerator();

        while (ftcols.MoveNext()){

            DataColumn dc = (DataColumn)ftcols.Current;
            ShowDebug(dc.Caption);
            ShowDebug(dc.DataType.ToString());
        
        }
        GridView1.DataSource = ft;
        GridView1.DataBind();

        Label5.Text = "Number of Matches: " + ft.Rows.Count;
        Label6.Visible = true;
        this.DrawMap();
     

    }

    private AcetateLayer CreateAcetateLayer(String AcetateName)
    {
        AcetateLayer alayer = new AcetateLayer(AcetateName);
        alayer.Name = AcetateName;
        alayer.Visible = true;
        layercollection.Add(alayer);
        mapview.Layers = layercollection;
        return alayer;

    }


    private void AddGeometry(ESRI.ArcGIS.ADF.IMS.Geometry.Geometry InputGeometry, AcetateLayer MyAcetateLayer)
    {
        AcetateElementCollection aec = MyAcetateLayer.AcetateElements;
        GeometryElement ge = new GeometryElement(AcetateUnits.Database);
        ge.Element = InputGeometry;

        SimpleMarkerSymbol sms = new SimpleMarkerSymbol();
        sms.Color = System.Drawing.Color.SpringGreen;
        sms.Type = MarkerSymbolType.Star;
        sms.OutlineColor = System.Drawing.Color.Black;
        sms.Width = 24;
        ge.Symbol = sms;

        aec.Add(ge);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        AcetateLayer alayer = (AcetateLayer)layercollection.FindByName("mygeocodepoints");
        if (alayer != null)
        {
            alayer.AcetateElements.Clear();
            layercollection.Remove(alayer);
        }

        GridView1.DataSource = null;
        GridView1.DataBind();
        this.DrawMap();
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
}