How to create a locator


This VBA code demonstrates how to create a new Locator. This Locator does not use an Alternate Street Name table or a Place Name Alias table. If the Locator is unable to find the required address fields in the feature class that you select, the Locator is not created.

How to use

  1. Paste this code into VBA in ArcCatalog.
  2. In the ArcCatalog tree, select a folder, an ArcSDE database connection (ArcEditor or ArcInfo only), or an Address Locators folder.
  3. Run the CreateLocator macro.
  4. When prompted, type a name for the new Locator.
  5. When prompted, browse for the reference data for this Locator.
[VBA]
Private Const MESSAGEBOX_TITLE = "CreateLocator Geocoding Developer Tip"

Public Sub CreateLocator()
    
    Const ERR_NUMBER_NAMETOOLONG = -2147220980
    Const LOCATORSTYLE_ARCSDE_USSTREETS = "SDE.US Streets"
    Const LOCATORSTYLE_FILE_USSTREETS = "US Streets (File)"
    Const LOCATORSTYLE_DATABASE_USSTREETS = "US Streets (GDB)"
    Const REFERENCEDATATABLE_NAME_PRIMARY = "Primary table"
    
    Dim pGxApplication As esriCatalogUI.IGxApplication '+++ reference to the parent ArcCatalog application
    Dim pGxObject As esriCatalog.IGxObject '+++ the selected object in ArcCatalog
    Dim pGxLocatorFolder As esriLocationUI.IGxLocatorFolder '+++ Address Locators folder
    Dim pLocatorWorkspace As esriGeoDatabase.ILocatorWorkspace '+++ LocatorWorkspace associated with the selected GxObject
    Dim pGxDatabase As esriCatalog.IGxDatabase '+++ a database object
    Dim pWorkspace As esriGeoDatabase.IWorkspace '+++ Workspace from which to obtain the LocatorWorkspace
    Dim pLocatorManager As esriLocation.ILocatorManager '+++ LocatorManager for getting LocatorWorkspace
    Dim pGxFile As esriCatalog.IGxFile '+++ IGxFile interface on a GxFolder
    Dim pLocator As esriGeoDatabase.ILocator '+++ reference to a locator style
    Dim strLocatorName As String '+++ name for the new Locator
    Dim pGxObjectFilter As esriCatalog.IGxObjectFilter '+++ ArcCatalog filter for the mini-browser
    Dim pDatasetName As esriGeoDatabase.IDatasetName '+++ IDatasetName interface on the Name object for the selected feature class
    Dim pWorkspaceName As esriGeoDatabase.IWorkspaceName '+++ Name object for the workspace that contains the feature class
    Dim pDatabaseLocatorWorkspace As esriLocation.IDatabaseLocatorWorkspace '+++ IDatabaseLocatorWorkspace interface on the LocatorWorkspace
    Dim pReferenceDataTables As esriLocation.IReferenceDataTables '+++ IReferenceDataTables interface on the Locator
    Dim pEnumReferenceDataTable As esriLocation.IEnumReferenceDataTable '+++ enumeration of reference data tables
    Dim i As Long '+++ loop counter
    Dim pReferenceDataTableEdit As esriLocation.IReferenceDataTableEdit '+++ IReferenceDataTableEdit interface on a reference data table
    Dim pNewLocator As esriGeoDatabase.ILocator '+++ Locator created by this script
    
    On Error GoTo ErrorHandler
    
    '+++ get a reference to the selected GxObject
    Set pGxApplication = ThisDocument.Parent
    Set pGxObject = pGxApplication.SelectedObject
    
    '+++ get the LocatorWorkspace from the selected GxObject
    If TypeOf pGxObject Is esriLocationUI.IGxLocatorFolder Then
        Set pGxLocatorFolder = pGxObject
        Set pLocatorWorkspace = pGxLocatorFolder.LocatorWorkspace
    ElseIf TypeOf pGxObject Is esriCatalog.IGxDatabase Then
        Set pGxDatabase = pGxObject
        If pGxDatabase.IsRemoteDatabase Then
            Set pWorkspace = pGxDatabase.Workspace
            Set pLocatorManager = New esriLocation.LocatorManager
            Set pLocatorWorkspace = pLocatorManager.GetLocatorWorkspace(pWorkspace)
        Else
            MsgBox "Cannot create a locator here.", vbCritical, MESSAGEBOX_TITLE
            Exit Sub
        End If
    ElseIf TypeOf pGxObject Is esriCatalog.IGxFolder Then
        Set pGxFile = pGxObject
        Set pLocatorManager = New esriLocation.LocatorManager
        Set pLocatorWorkspace = pLocatorManager.GetLocatorWorkspaceFromPath("")
    Else
        MsgBox "Cannot create a locator here.", vbCritical, MESSAGEBOX_TITLE
        Exit Sub
    End If
    
    '+++ get the US Streets LocatorStyle from the LocatorWorkspace
    If TypeOf pLocatorWorkspace Is esriLocation.IDatabaseLocatorWorkspace Then
        Set pLocator = pLocatorWorkspace.GetLocatorStyle(LOCATORSTYLE_ARCSDE_USSTREETS)
    ElseIf TypeOf pLocatorWorkspace Is esriLocation.ILocalLocatorWorkspace Then
        '+++ be default, choose the LocatorStyle for database reference data sources
        Set pLocator = pLocatorWorkspace.GetLocatorStyle(LOCATORSTYLE_DATABASE_USSTREETS)
    End If
    
    '+++ prompt the user for a name for the new Locator
    strLocatorName = InputBox("Enter a name for the new address locator:", MESSAGEBOX_TITLE, "New Address Locator")
    If strLocatorName = "" Then Exit Sub
    
    '+++ get the primary reference data feature class for the Locator
    If TypeOf pLocatorWorkspace Is esriLocation.IDatabaseLocatorWorkspace Then
        Set pGxObjectFilter = New esriCatalog.GxFilterSDEFeatureClasses
    ElseIf TypeOf pLocatorWorkspace Is esriLocation.ILocalLocatorWorkspace Then
        Set pGxObjectFilter = New esriCatalog.GxFilterFeatureClasses
    End If
    '+++ browse for the feature class
    Do
        Do
            Set pDatasetName = BrowseForFeatureClass(pGxObjectFilter)
            If pDatasetName Is Nothing Then Exit Sub
            '+++ validate the feature class that was selected
            If TypeOf pLocatorWorkspace Is esriLocation.IDatabaseLocatorWorkspace Then
                '+++ verify that the selected feature class is in the same workspace as the locator
                Set pWorkspaceName = pDatasetName.WorkspaceName
                Set pDatabaseLocatorWorkspace = pLocatorWorkspace
                If pDatabaseLocatorWorkspace.Workspace.ConnectionProperties.IsEqual(pWorkspaceName.ConnectionProperties) Then
                    Exit Do
                Else
                    If MsgBox("Choose a feature class in the same workspace as the address locator.", vbOKCancel, _
                              MESSAGEBOX_TITLE) = vbCancel Then Exit Sub
                End If
            ElseIf TypeOf pLocatorWorkspace Is esriLocation.ILocalLocatorWorkspace Then
                '+++ select the correct LocatorStyle if this is a file-based reference data source
                Set pWorkspaceName = pDatasetName.WorkspaceName
                If pWorkspaceName.Type = esriFileSystemWorkspace Then
                    Set pLocator = pLocatorWorkspace.GetLocatorStyle(LOCATORSTYLE_FILE_USSTREETS)
                End If
                Exit Do
            End If
        Loop
        
        '+++ set the primary reference data table for the locator
        Set pReferenceDataTables = pLocator
        Set pEnumReferenceDataTable = pReferenceDataTables.Tables
        pEnumReferenceDataTable.Reset
        For i = 1 To pEnumReferenceDataTable.Count
            Set pReferenceDataTableEdit = pEnumReferenceDataTable.Next
            If pReferenceDataTableEdit.DisplayName = REFERENCEDATATABLE_NAME_PRIMARY Then
                Set pReferenceDataTableEdit.Name = pDatasetName
            End If
        Next i
        If Not pReferenceDataTables.HasEnoughInfo Then
            If MsgBox("Check the feature class to see if it contains the required fields for this address locator style.", _
                      vbOKCancel, MESSAGEBOX_TITLE) = vbCancel Then Exit Sub
        Else
            Exit Do
        End If
    Loop
    
    '+++ add the Locator to the LocatorWorkspace
    If TypeOf pGxObject Is esriCatalog.IGxFolder Then
        Set pLocatorWorkspace = pLocatorManager.GetLocatorWorkspaceFromPath(pGxFile.Path)
    End If
    Set pNewLocator = pLocatorWorkspace.AddLocator(strLocatorName, pLocator, "")
    
    '+++ if the selected GxObject is a GxFolder or GxLoctorFolder, then refresh it
    pGxObject.Refresh
    
    Exit Sub
    
ErrorHandler:
    MsgBox "An unexpected error occurred." & vbNewLine & Err.Number & ": " & Err.Description, vbCritical, MESSAGEBOX_TITLE, Err.HelpFile, Err.HelpContext
    If Err.Number = ERR_NUMBER_NAMETOOLONG Then
        '+++ prompt the user for a shorter name for the locator, then resume execution
        strLocatorName = PromptForLocatorName(strLocatorName)
        If strLocatorName = "" Then Exit Sub
        Resume
    End If
    
End Sub

Private Function BrowseForFeatureClass(pGxObjectFilter As esriCatalog.IGxObjectFilter) As esriSystem.IName
    
    Const GXDIALOG_BUTTON = "Open"
    Const GXDIALOG_TITLE = "Select the reference data feature class..."
    
    Dim pGxDialog As esriCatalogUI.IGxDialog '+++ ArcCatalog mini-browser dialog
    Dim pEnumGxObject As esriCatalog.IEnumGxObject '+++ enumeration of selected objects
    Dim pGxObject As esriCatalog.IGxObject '+++ selected GxObject
    
    '+++ create the GxDialog and browse for feature classes
    Set pGxDialog = New esriCatalogUI.GxDialog
    With pGxDialog
        .ButtonCaption = GXDIALOG_BUTTON
        .Title = GXDIALOG_TITLE
        Set .ObjectFilter = pGxObjectFilter
        If .DoModalOpen(ThisDocument.Parent.hWnd, pEnumGxObject) = False Then Exit Function
    End With
    
    pEnumGxObject.Reset
    Set pGxObject = pEnumGxObject.Next
    Set BrowseForFeatureClass = pGxObject.InternalObjectName
    
End Function






Additional Requirements
  • A feature class that can be used as reference data for a US Streets address locator