Geocode single address using an SDE locator


This sample demonstrates the basic steps of using an SDE locator to geocode single address and to show the result in ArcMap.

How to use:
  1. Create a geocoding service on ArcSDE server (choosing SDE.Single Field). Add the corresponding data to ArcMap.
  2. Copy and paste the code into ArcMap VB Editor.
  3. In LocateDriver subroutine, change the ArcSDE connection information, searchString, locatorName, zoomFactor, and index of the data layer used.
  4. Run LocateDriver subroutine, the map will zoom to the found feature.
Public Sub locateDriver()
 'To demonstrate the usage of the locating functions
   Dim pMxDoc As esriCore.IMxDocument
  Set pMxDoc = ThisDocument
  Dim pMap As esriCore.IMap
  Set pMap = pMxDoc.FocusMap
  Dim searchString As String
  searchString = "What do you want to search"
  Dim locatorName As String
  locatorName = "Your locator Name"
  Dim pWorkSpace As IWorkspace
  Set pWorkSpace = getSDEWorkSpace("server_name", "instance_name", "database_name", "user_name", "password", "version")
  Dim pResult As IPropertySet
  Set pResult = getMatchingResult(searchString, locatorName, pWorkSpace)
  Dim pLayer As IFeatureLayer
  Set pLayer = pMap.Layer(0)  ' the data layer you are using
   Dim zoomFactor As Double
  zoomFactor = 1.2
  showMatchingResult pResult, pLayer, zoomFactor
End Sub

Private Function getSDEWorkSpace(server_name As String, _
                                 instance_name As String, _
                                 database_name As String, _
                                 user_name As String, _
                                 password As String, _
                                 version As String) As IWorkspace
  Dim pPropset As IPropertySet
  Set pPropset = New PropertySet
  With pPropset
    .SetProperty "Server", server_name
    .SetProperty "Instance", instance_name
    .SetProperty "Database", database_name
    .SetProperty "user", user_name
    .SetProperty "password", password
    .SetProperty "version", version
  End With
  Dim pFactory As IWorkspaceFactory
  Set pFactory = New SdeWorkspaceFactory
  Set getSDEWorkSpace = pFactory.Open(pPropset, 0)
End Function

Private Function getMatchingResult(searchString As String, locatorName As String, pWorkSpace As IWorkspace) As IPropertySet
  Dim pLocatorManager As esriCore.ILocatorManager
  Set pLocatorManager = New esriCore.LocatorManager
   'Get LocatorWorkspace with Locators
   Dim pLocatorWorkspace As esriCore.ILocatorWorkspace
  Set pLocatorWorkspace = pLocatorManager.GetLocatorWorkspace(pWorkSpace)
   'Get Specific Locator
   Dim pLocator As esriCore.ILocator
  Set pLocator = pLocatorWorkspace.GetLocator(locatorName)
   'Get IAddressGeocoding interface pointer to Match Address
   Dim pAddressGeocoding As esriCore.IAddressGeocoding
  Set pAddressGeocoding = pLocator
   'use a PropertySet to hold results from Address Match
   Dim pPropset As IPropertySet
  Set pPropset = getInputPropertySet(searchString)
  Set getMatchingResult = pAddressGeocoding.MatchAddress(pPropset)
End Function

Private Function getInputPropertySet(searchString) As IPropertySet
  Dim pPropset As esriCore.IPropertySet
  Set pPropset = New esriCore.PropertySet
  pPropset.SetProperty "KeyField", searchString
  Set getInputPropertySet = pPropset
End Function

Private Sub showMatchingResult(pResult As IPropertySet, pLayer As IFeatureLayer, zoomFactor As Double)
 'use ThisDocument and Application
   Dim pGeometry As esriCore.IGeometry
  If TypeOf pResult.GetProperty("SHAPE") Is esriCore.IGeometry Then
    Set pGeometry = pResult.GetProperty("SHAPE")
    MsgBox "The PropertySet is not valid."
    Exit Sub
  End If
   'change the projection of the point to the projection of current map
   Dim pPt As IPoint
  Set pPt = pGeometry
  Dim pMxDoc As IMxDocument
  Set pMxDoc = ThisDocument
  Dim pMap As IMap
  Set pMap = pMxDoc.FocusMap
  Dim pSpRef_map As esriCore.ISpatialReference
  Set pSpRef_map = pMap.SpatialReference
  Dim pSpRef_data As esriCore.ISpatialReference
  Dim pGeoDataset As IGeoDataset
  Set pGeoDataset = pLayer
  Set pSpRef_data = pGeoDataset.SpatialReference
  Dim pG As esriCore.IGeometry
  Set pG = pPt
  Set pG.SpatialReference = pSpRef_data
  pG.Project pSpRef_map
  showStatus pResult, pPt
  Dim pMxApp As IMxApplication
  Set pMxApp = Application
   'select the feature found
   pMap.SelectByShape pPt, pMxApp.SelectionEnvironment, False
  zoomToFirstSelected zoomFactor
End Sub

Private Sub zoomToFirstSelected(zoomFactor As Double)
  Dim pMxDoc As IMxDocument
  Set pMxDoc = ThisDocument
  Dim pMap As IMap
  Set pMap = pMxDoc.FocusMap
  Dim pEnumFeature As IEnumFeature
  Set pEnumFeature = pMap.FeatureSelection
  Dim pEnv As IEnvelope
  Dim pFeature As IFeature
  Set pFeature = pEnumFeature.Next
  If Not pFeature Is Nothing Then
    Set pEnv = pFeature.Shape.Envelope
    pEnv.Expand zoomFactor, zoomFactor, True
    pMxDoc.ActivatedView.Extent = pEnv
  End If
End Sub

Private Sub showStatus(pResult As IPropertySet, pPt As IPoint)
  Application.StatusBar.Message(0) = "Match Score: " & pResult.GetProperty("Score") & _
                                     " X:" & Format(pPt.X, "#, ###.##") & _
                                     " Y:" & Format(pPt.Y, "#, ###.##")
End Sub