Network Analyst routing
ArcGIS_Routing_VBNet\Default.aspx.vb
' Copyright 2010 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 Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Xml
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer
Imports ESRI.ArcGIS.ADF.Web.Geocode
Imports ESRI.ArcGIS.ADF.Web.DataSources

Namespace RouteFinder
  Partial Public Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
      If Session.IsNewSession Then
        Session.Add("ModifiedNAContextApplied", False)
      End If
    End Sub

    ''' <summary>
    ''' Creates an array of x,y coordinates for the passed locationString.
    ''' </summary>
    ''' <returns></returns>
    Private Sub GeocodeAddress(ByVal igf As IGeocodeFunctionality, ByVal address As String, ByVal zip As String, <System.Runtime.InteropServices.Out()> ByRef x As Double, <System.Runtime.InteropServices.Out()> ByRef y As Double)
      x = Double.NaN
      y = Double.NaN

      Dim avc As System.Collections.Generic.List(Of AddressValue) = New System.Collections.Generic.List(Of AddressValue)()
      Dim av1 As AddressValue = New AddressValue("STREET", address)
      Dim av2 As AddressValue = New AddressValue("ZONE", zip)
      avc.Add(av1)
      avc.Add(av2)

      Dim geocodedPoint As ESRI.ArcGIS.ADF.Web.Geometry.Point = igf.GeocodeAddress(avc)
      x = geocodedPoint.X
      y = geocodedPoint.Y

      Return
    End Sub

    ''' <summary>
    ''' Updates the form elements with values saved in session
    ''' </summary>

    Private Sub GetPreviousParameters()
      If Session("FromAddress") Is Nothing Then
        Return
      End If

      FromStreet.Value = Session("FromAddress").ToString()
      FromZip.Value = Session("FromZip").ToString()
      ToStreet.Value = Session("ToAddress").ToString()
      ToZip.Value = Session("ToZip").ToString()
    End Sub

    Protected Sub btnGetDirections_Click(ByVal sender As Object, ByVal e As System.EventArgs)
      If (Not GeocodeResourceManager1.Initialized) Then
        GeocodeResourceManager1.Initialize()
      End If

      Dim gisResource As IGISResource = GeocodeResourceManager1.GetResource(0)
      If gisResource Is Nothing Then
        Throw New Exception("Error getting Geocode resource.  Make sure you have set up the identity in web.config")
      End If

      Dim supported As Boolean = gisResource.SupportsFunctionality(GetType(ESRI.ArcGIS.ADF.Web.DataSources.IGeocodeFunctionality))

      If supported Then
        Dim igf As IGeocodeFunctionality = CType(gisResource.CreateFunctionality(GetType(IGeocodeFunctionality), Nothing), IGeocodeFunctionality)

        Dim fromX, fromY, toX, toY As Double

        Session("FromAddress") = FromStreet.Value.ToString()
        Session("FromZip") = FromZip.Value.ToString()
        Session("ToAddress") = ToStreet.Value.ToString()
        Session("ToZip") = ToZip.Value.ToString()

        'geocode addresses
        GeocodeAddress(igf, FromStreet.Value.ToString(), FromZip.Value.ToString(), fromX, fromY)
        GeocodeAddress(igf, ToStreet.Value.ToString(), ToZip.Value.ToString(), toX, toY)


        If (Double.IsNaN(toX)) OrElse (Double.IsNaN(fromX)) Then
          AddressErrorText.Visible = True
          If Double.IsNaN(fromX) Then
            StartingErrorText.Visible = True
          End If
          If Double.IsNaN(toX) Then
            EndingErrorText.Visible = True
          End If
          GetPreviousParameters()
          Return
        End If
        'pass points to directions form
        Dim url As String = String.Format("Directions.aspx?FromX={0}&FromY={1}&ToX={2}&ToY={3}", fromX, fromY, toX, toY)
        Response.Redirect(url, True)
      End If
    End Sub

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

    Private Function CapitalizeFirstLetters(ByVal inString As String) As String
      Dim words As String() = inString.Split(Char.Parse(" "))
      Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
      Dim s, s2 As String
      Dim i As Integer = 0
      Do While i < words.Length
        s = words(i).Substring(0, 1)
        If words(i).Length > 1 Then
          s2 = words(i).Substring(1)
        Else
          s2 = ""
        End If
        If i <> 0 Then
          sb.Append(" ")
        End If
        sb.Append(s.ToUpper() & s2)
        i += 1
      Loop
      Return sb.ToString()
    End Function


  End Class
End Namespace