ArcIMS Geocoding
ArcIMS_Geocoding_VBNet\Default.aspx.vb
' 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.
' 




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

Partial Class _Default
    Inherits System.Web.UI.Page

    Private appservername As String = "adriatic"
    Private appserverport As Integer = 5300
    Private servicename As String = "RoadCenterline_Locator"
    Private geocodelayername As String = "RoadCenterline"

    Private serverconnection As TCPConnection
    Private mapservice As MapService
    Private mapview As MapView
    Private layercollection As LayerCollection

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then
            If Session.IsNewSession Then

                serverconnection = New TCPConnection(appservername, appserverport)
                serverconnection.ServiceName = servicename
                mapservice = New MapService(serverconnection, New InitializationParameters(), True)
                mapview = mapservice.CreateMapView()
                Dim imgdesc As ESRI.ArcGIS.ADF.IMS.Carto.ImageDescriptor = mapview.ImageDescriptor

                imgdesc.Height = CType(((Image1.Height).Value), Integer)
                imgdesc.Width = CType(((Image1.Width).Value), Integer)

                layercollection = mapview.Layers

                Me.DrawMap()
                Me.DrawLegend()

                Session.Add("sc", serverconnection)
                Session.Add("ms", mapservice)
                Session.Add("mv", mapview)

            End If
        Else
            serverconnection = CType(Session("sc"), TCPConnection)
            mapservice = CType(Session("ms"), MapService)
            mapview = CType(Session("mv"), MapView)
            layercollection = mapview.Layers
        End If
    End Sub

    Private Sub DrawMap()
        mapview.Draw()
        Dim cartoimage As CartoImage = mapview.Image
        Me.Image1.ImageUrl = cartoimage.Url
    End Sub

    Private Sub DrawLegend()
        Dim legend As Legend = mapview.Legend
        legend.Height = CType(((Image2.Height).Value), Integer)
        legend.Width = CType(((Image2.Width).Value), Integer)
        legend.Draw()
        Dim legendimage As CartoImage = legend.Image
        Me.Image2.ImageUrl = legendimage.Url

    End Sub

    Private Sub ShowDebug(ByVal message As String)
        System.Diagnostics.Debug.WriteLine(message)

    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim layer As Layer = layercollection.FindByName(geocodelayername)
        Dim flayer As FeatureLayer = CType(layer, FeatureLayer)

        Dim geocoder As Geocoder = flayer.Geocoder
        geocoder.MaxCandidates = Int32.Parse(DropDownList2.Text)
        geocoder.MinScore = Int32.Parse(DropDownList1.Text)

        'ShowDebug(geocoder.Style)

        'For Each inf As InputField In geocoder.InputFields
        '    ShowDebug(inf.Description)
        '    ShowDebug(inf.ID)
        '    ShowDebug(inf.Label)
        'Next

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

        Dim alayer As AcetateLayer = CType(layercollection.FindByName("mygeocodepoints"), AcetateLayer)

        If ft.Rows.Count > 0 Then
            If Not alayer Is Nothing Then
                alayer.AcetateElements.Clear()
            Else
                alayer = Me.CreateAcetateLayer("mygeocodepoints")
            End If

            For Each dr As DataRow In ft.Rows
                Dim ss As Integer = CType(dr("SCORE"), Integer)
                Dim af As String = CType(dr("ADDRESSFOUND"), String)
                Dim pnt As ESRI.ArcGIS.ADF.IMS.Geometry.Point = CType(dr("#SHAPE#"), ESRI.ArcGIS.ADF.IMS.Geometry.Point)
                Me.AddGeomeTry(pnt, alayer)
            Next

        ElseIf Not alayer Is Nothing Then
            alayer.AcetateElements.Clear()
            layercollection.Remove(alayer)
        End If

        'For Each dc As DataColumn In ft.Columns
        '    ShowDebug(dc.Caption)
        '    ShowDebug(dc.DataType.ToString())
        'Next

        GridView1.DataSource = ft
        GridView1.DataBind()

        Label5.Text = "Number of Matches: " + ft.Rows.Count.ToString()
        Label6.Visible = True
        Me.DrawMap()


    End Sub

    Private Function CreateAcetateLayer(ByVal AcetateName As String) As AcetateLayer
        Dim alayer As AcetateLayer = New AcetateLayer(AcetateName)
        alayer.Name = AcetateName
        alayer.Visible = True
        layercollection.Add(alayer)
        mapview.Layers = layercollection
        Return alayer

    End Function


    Private Sub AddGeomeTry(ByVal InputGeomeTry As ESRI.ArcGIS.ADF.IMS.Geometry.Geometry, ByVal MyAcetateLayer As AcetateLayer)
        Dim aec As AcetateElementCollection = MyAcetateLayer.AcetateElements
        Dim ge As GeomeTryElement = New GeomeTryElement(AcetateUnits.Database)
        ge.Element = InputGeomeTry

        Dim sms As SimpleMarkerSymbol = 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)
    End Sub
    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim alayer As AcetateLayer = CType(layercollection.FindByName("mygeocodepoints"), AcetateLayer)
        If Not alayer Is Nothing Then
            alayer.AcetateElements.Clear()
            layercollection.Remove(alayer)
        End If

        GridView1.DataSource = Nothing
        GridView1.DataBind()
        Me.DrawMap()
    End Sub

End Class