English

How To: Add a geocoding service to ArcMap with ArcObjects

Summary

The following code shows how to add an existing geocoding service to an ArcMap document and make it the current locator.

Procedure

This sample code assumes the geocoding service is located in the top-level Geocoding Services folder in ArcCatalog. If the file is located in a different folder, it is necessary to modify the code by entering the folder's path in the following line of code:

Code:
Set pLocWS = pLocManager.GetLocatorWorkspaceFromPath("")

An example would be:

Code:
Set pLocWS = pLocManager.GetLocatorWorkspaceFromPath("d:\work")


If you are working with an ArcGIS Server Geocoding Service, specify the name of the service and be sure to add a reference to the ESRI GIS Client Library.

  1. Start ArcMap.
  2. Open the Visual Basic Editor.

    In ArcMap, select Tools > Macros > Visual Basic Editor.

  3. In the Project Explorer window, expand Project.mxt and select ArcMap Objects > ThisDocument, right-click, and select View Code.
    [O-Image] Visual Basic Project  Explorer

    Note:
    Code in the Project's ThisDocument code module only runs in the current map document. To store the code in all of the map documents, open the Normal.mxt ThisDocument code module instead.

  4. Copy the following code into the code module:

    Code:
    Public Sub test()
    AddLocatorToArcMapAndMakeItTheCurrentLocator "robe2211.test_gs"
    End Sub

    Private Sub AddLocatorToArcMapAndMakeItTheCurrentLocator(locName As String)
    Dim pUID As UID
    Set pUID = New UID
    pUID.Value = "esricore.locatorextension"
    Dim pLocatorExtension As ILocatorExtension
    Set pLocatorExtension = Application.FindExtensionByCLSID(pUID)
    Dim pLocManager As ILocatorManager2
    Set pLocManager = New LocatorManager
    Dim pLocWS As ILocatorWorkspace
    'This line of code is for standard Geocoding Services
    Set pLocWS = pLocManager.GetLocatorWorkspaceFromPath("")
    'If you are working with Server comment out the line above and uncomment the following lines.
    'Dim pAGSConn As IAGSServerConnectionName
    'Set pAGSConn = New AGSServerConnectionName
    'Dim pProps As IPropertySet2
    'Set pProps = New PropertySet
    'pProps.SetProperty "machine", "testserver"
    'pAGSConn.ConnectionProperties = pProps
    'Dim pName As IName
    'Set pName = pAGSConn
    'Dim pAGSServerConnection As IAGSServerConnection
    'Set pAGSServerConnection = pName.Open
    'Set pLocWS = pLocManager.GetAGSLocatorWorkspace(pAGSConn)
    Dim pLoc As ILocator
    Set pLoc = pLocWS.GetLocator(locName)
    Dim lng As Long
    lng = pLocatorExtension.AddLocator(pLoc)
    pLocatorExtension.CurrentLocator(pLoc.Category) = lng
    End Sub

  5. Close the Visual Basic Editor.
  6. Run the code.

    A. Click Tools > Macros > Macros to display the Macros dialog box.
    B. Select a macro and click Run.