English

How To: Geocode an address using the ArcIMS Java Connector

Summary

This article provides sample JSP code that demonstrates the methods required to provide geocoding functionality using the ArcIMS Java Connector.

Procedure

  1. Install and configure ArcIMS JSP samples. Proceed to the next step only after verifying that the samples can be run with SantaClara service.
Note:
JSP samples can be installed from ArcIMS CD. After installing, see the readme file in <ArcIMS_ Home>/Samples/Java folder for configuring and running the samples.
  1. Navigate to the ArcIMS_Home\Samples\TutorialData\Axl folder. Open the santaclara.axl file in Author.
  2. Select sc_streets layer and click Geocoding Properties. Select the 'Address Style' as 'US Streets with Zone'. Check 'Build Geocoding Index' and click OK to build Geocoding Index files. Save the file and close Author.
Note:
The Java Connector will only recognize one AXL EXTENSION tag. For the <EXTENSION type="Geocode"> required for geocoding, edit the .AXL configuration file, removing the two instances of the Extract server extensions. Look for the lines of AXL and comment out each line; two instances are in the sample SantaClara.axl.
Change the section:
<EXTENSION type="Extract">
  <EXTRACTPARAMS clip="true" />
</EXTENSION>
to:
<!--
<EXTENSION type="Extract">
  <EXTRACTPARAMS clip="true" />
</EXTENSION>
-->
  1. Open ArcIMS Administrator and look for 'SantaClara' image service. If there, refresh the service. If not, create a new 'SantaClara' image service. Save the site configuration.
  2. Create a JSP page named 'geocode.jsp' using the code below:
    <%@ page contentType="text/html"%>
    <%@ page import="com.esri.aims.mtier.model.map.layer.FeatureLayer" %>
    <%@ page import="com.esri.aims.mtier.model.map.Layers" %>
    <%@ page import="com.esri.aims.mtier.model.map.layer.geocode.*" %>
    <%@ page import="com.esri.aims.mtier.model.envelope.Envelope" %>
    <%@ page import="com.esri.aims.mtier.model.acetate.Point" %>
    <%@ page import="com.esri.aims.mtier.model.acetate.Points" %>
    <%@ page import="com.esri.aims.mtier.model.map.layer.renderer.symbol.SimplePolygonSymbol" %>
    
    <jsp:useBean id="cp" class="com.esri.aims.mtier.io.ConnectionProxy" scope="page" />
    <jsp:useBean id="map" class="com.esri.aims.mtier.model.map.Map" scope="page" />
    
    
    <%
    // identify constants for map server, map service and streets layer name
    final String MAP_SERVER = "localhost";
    final String MAP_SERVICE = "SantaClara";
    final String STREETS_LAYER = "sc_streets";
    
    String action = request.getParameter("action");
    String x = request.getParameter("map.x");
    String y = request.getParameter("map.y");
    String mapImageUrl = "";
    
    // set default values for address
    String address = "2000 Park Blvd.";
    String zip = "94306";
    if (request.getParameter("address") != null) {
    	address = request.getParameter("address");
    	}
    if (request.getParameter("zip") != null) {
    	zip = request.getParameter("zip");
    	}
    	
    
    // create connection
    if(cp.getHost() == null){
    	cp.setHost(MAP_SERVER);
          cp.setPort(5300);
          cp.setService(MAP_SERVICE);
          map.initMap(cp, 0, true, true, true, true);
          map.setWidth(500);
          map.setHeight(350);
          } 
      
    // if form was submitted...
    if(action != null){
          //get street layer
          FeatureLayer f=null;
          Layers layers = map.getLayers();
          for ( int i=0;i<layers.getCount();i++){
          // find the streets layer we'll be using to geocode against
          if (layers.item(i).getName().equalsIgnoreCase(STREETS_LAYER)){
          	f = (FeatureLayer) layers.item(i);
          	break;
          	}	// end if layer is streets layer
    	}    // end for each layer
      
         // create the AddressMatchInputs to store the address information and preferences
         AddressMatchInputs ami= f.getAddressMatchInputs();
         ami.setMinScore(20);
         ami.setMaxCandidates(10); 
         ami.moveFirst();
         ami.addID("STREET");
         ami.addValues(address);
         ami.moveNext();
         ami.addID("ZIP");
         ami.addValues(zip); 
        
        // refresh the map, sends the GET_GEOCODE request
        map.refresh();
        
        // after the refresh, the AddressMatchResults will have data
        AddressMatchResults amr = ami.getAddressMatchResults();
        
        if (amr!=null && amr.getCount() > 0){
        	//get first result
         	Result result = amr.getResult(0);
         	Envelope envelope = new Envelope();
         	Point point = result.getPointObject();
         
         	//set envelope based on the result
         	envelope.setMaxX(point.getX() + .009);
         	envelope.setMaxY(point.getY() + .009);
         	envelope.setMinX(point.getX() - .009);
         	envelope.setMinY(point.getY() - .009);
    
         	//create an acetate layer and add the geocoded point
         	com.esri.aims.mtier.model.map.layer.AcetateLayer al = new com.esri.aims.mtier.model.map.layer.AcetateLayer("aclayer1",f.getMaxScale(),f.getMinScale());
         	com.esri.aims.mtier.model.acetate.Acetate acetate = new  com.esri.aims.mtier.model.acetate.Acetate();
         	acetate.setUnits(com.esri.aims.mtier.model.acetate.Acetate.DATABASE);
         	acetate.setAcetateElement(point);
         	al.add(acetate);
        
         	//add text label for the address to the acetate layer
         	com.esri.aims.mtier.model.map.layer.renderer.symbol.TextMarkerSymbol tms= new com.esri.aims.mtier.model.map.layer.renderer.symbol.TextMarkerSymbol();
         	tms.setFontColor("0,0,0");
         	tms.setFont("arial");
         	tms.setOutline("255,255,0");
         	tms.setGlowing("255,255,125"); 
         	tms.setFontSize(12);
         	tms.setAntialiasing(true);
         	tms.setFontStyle("BOLD");  
        
         	com.esri.aims.mtier.model.acetate.Text text= new com.esri.aims.mtier.model.acetate.Text();
         	text.setLabel(address+"  "+zip);
         	text.setLabel(result.getValue());
         	text.setSymbol(tms);
         	text.setX(point.getX());
         	text.setY(point.getY()+.0008);
         	com.esri.aims.mtier.model.acetate.Acetate acetate1 = new  com.esri.aims.mtier.model.acetate.Acetate();
         	acetate1.setUnits(com.esri.aims.mtier.model.acetate.Acetate.DATABASE);
         	acetate1.setObject(text);
         	al.add(acetate1); 
         
         	//add acetate layer and zoom to the geocoded point extent
         	map.getLayers().add(al);     
         	map.doZoomToExtent(envelope); 
         	}	// end if found an address
         else {
         	out.print ("Did not locate address: " + address + " " + zip);
         	}	
     }	// end if form submitted
    
    
    
    // refresh the map sending a GET_IMAGE request
    map.refresh();
    mapImageUrl = map.getMapOutput().getURL();
    
    %>
    
    
    <HTML>
    <HEAD>
    <TITLE>JSP Geocode Sample</TITLE>
    </HEAD>
    <BODY>
    
    <form action="geocode.jsp">
    <table border="1">
    <tr>
    	<td align="center"><!-- Map Image -->
    	<img name="map" src="<%=mapImageUrl%>" width="400" height="350">
    	</td>
    	<td valign="top">
    		<table border="0">
    		<tr>
    			<td>Address:</td>
    			<td>
    			<input type="text" name="address" size="20" value="<%=address%>">
    			</td>
    		</tr>
    		<tr>
    			<td>Zip Code:</td>
    			<td>
    			<input type="text" name="zip" size="8" value="<%=zip%>">
    			</td>
    		</tr>
    		<tr>
    			<td colspan="2" align="center">
    			<input type="submit" name="action" value="Find Address">
    			</td>
    		</tr>
    		</table>
    	</td>
    </tr>
    </table>
    </form>
    </BODY>
    </HTML>
  3. Load this geocode.jsp in the same way as other sample-JSP pages are run, as described in Step 1.