English

How To: Geocode using Java Connector using the RouteServer Extension

Summary

This article provides sample JSP code to implement geocoding functionality using the ArcIMS Java Connector and ArcIMS RouteServer Extension. This sample does not use the Geocode Virtual Server; this approach could therefore be extended to perform reverse geocoding or routing using the RouteServer Extension.

Procedure

  1. Install and configure the ArcIMS JSP samples. The JSP samples can be installed from the ArcIMS CD. After installing, see the readme file in <ArcIMS_ Home>/Samples/Java/ folder for instructions on configuring and running the samples.
    Note:
    Proceed to the next step only after verifying that the samples can be run with the sample SantaClara map service.
  2. Install and configure the RouteServer HTML Viewer client sample. Refer to the installation documentation on the RouteServer CD for instructions.

    Once steps 1 and 2 are complete, there should be two sample map services running: an ArcIMS ImageServer map service named 'USA' and a RouteServer map service named 'RouteService'.
  3. Create a custom JSP page to accomplish the desired functionality (geocoding, reverse gecoding, routing, and so on).

    The following is a conceptual overview of the steps for creating standard geocoding functionality. The same concepts and techniques can be applied to achieve reverse geocoding or routing functionality.
    1. Define two Connections, one for each map service (ImageServer and RouteServer).
    2. Build and send an ArcXML GET_GEOCODE request to the RouteServer.
    3. Parse the response to get the X and Y values of the geocoded point.
    4. Use ArcIMS API calls to pan/zoom to the geocoded point location, draw the point on an acetate layer, and refresh the map image.
  4. To create a working example:
    1. Create a JSP page named 'geocodeRS.jsp' using this code:
      <%@ page contentType="text/html; charset=ISO-8859-1" %>
      <%@ page import="com.esri.aims.mtier.io.*, com.esri.aims.mtier.model.map.layer.FeatureLayer" %>
      <%@ page import="com.esri.aims.mtier.model.acetate.Point" %>
      <%@ page import="com.esri.aims.mtier.model.envelope.Envelope" %>
      <%@ page import="com.esri.aims.mtier.model.map.layer.renderer.symbol.SimpleMarkerSymbol" %>
      <%@ page import="com.esri.aims.mtier.model.map.layer.AcetateLayer" %>
      <%@ page import="com.esri.aims.mtier.model.acetate.Acetate" %>
      
      <jsp:useBean id="r_connection" class="com.esri.aims.mtier.io.ConnectionProxy" scope="page" />
      <jsp:useBean id="i_connection" class="com.esri.aims.mtier.io.ConnectionProxy" scope="page" />
      <jsp:useBean id="map" class="com.esri.aims.mtier.model.map.Map" scope="page" />
      	
      <%
      // Set constants for map server, map service and streets layer name.
      final String MAP_SERVER = "myserver";
      final String IMAGE_MAP_SERVICE = "USA";
      final String ROUTE_MAP_SERVICE = "RouteService";
      	
      String mapImageUrl = "";
      	
      // Set default values for the address to be geocoded.
      String gaddress = "380 New York St.";
      String zip = "92373";
      String action = request.getParameter("action");
      	
      if (request.getParameter("address") != null) {
      	gaddress = request.getParameter("address");
      	}
      if (request.getParameter("zip") != null) {
      	zip = request.getParameter("zip");
      	}
      	
      	// Create a connection to the ImageServer map service.
      	i_connection.setHost(MAP_SERVER);
      	i_connection.setPort(5300);
      	i_connection.setService(IMAGE_MAP_SERVICE);
      	//i_connection.setDisplayMessages(false);
      	
      	// Instantiate the map.
      	map.setWidth(500);
      	map.setHeight(350);
      	map.initMap(i_connection,0,false,false,false,false);
      	map.getLegend().setWidth(200);
      	map.getLegend().setHeight(150);
      	map.getLegend().setTitle("My Map");
      	map.refresh();
      	mapImageUrl = map.getMapOutput().getURL();
      	
      if(action != null){
      	
      	// Create a connection to the RouteServer.
      	r_connection.setHost(MAP_SERVER);
      	r_connection.setPort(5300);
      	r_connection.setService(ROUTE_MAP_SERVICE);
      	r_connection.setDisplayMessages(false);
      	
      	/* Programmatically create the following GET_GEOCODE request:
      	<GET_GEOCODE maxcandidates="20" minscore="60">
      	<LAYER id="18" />
      	<ADDRESS>
      	<GCTAG id="Street Address" value="380 New York St"/>
      	<GCTAG id="5-digit ZIP Code" value="92373"/>
      	</ADDRESS>
      	</GET_GEOCODE>
      	*/
      	
      	// Programmatically creating the GET_GEOCODE request here...
      	String g_request = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
      	g_request=g_request +"<ARCXML version=\"1.1\"><REQUEST><GET_GEOCODE ";
      	g_request=g_request +" maxcandidates=\"20\" minscore=\"60\"><LAYER id=\"18\" />";
      	g_request=g_request +"<ADDRESS><GCTAG id=\"Street Address\" value=\"";
      	g_request=g_request + gaddress + "\"/><GCTAG id=\"5-digit ZIP Code\" value=\"" + zip;
      	g_request=g_request + "\"/></ADDRESS>";
      	g_request=g_request + "</GET_GEOCODE></REQUEST></ARCXML>";
      	
      	// For debugging purposes...
      	//out.print(g_request);
      	
      	// Send the request to the RouteServer.
      	String g_response = r_connection.send(g_request);
      	
      	// Parse the response to extract the X and Y of the geocoded point.
      	int point = g_response.indexOf("<POINT");
      	
      	int beginXGPoint = g_response.indexOf("x", point) + 3;
      	int endXGPoint = g_response.indexOf("\"", beginXGPoint);
      	String xPoint = g_response.substring(beginXGPoint, endXGPoint);
      	//out.print(xPoint);
      	double x = Double.parseDouble(xPoint);
      	
      	int beginYGPoint = g_response.indexOf("y", point) + 3;
      	int endYGPoint = g_response.indexOf("\"", beginYGPoint);
      	String yPoint = g_response.substring(beginYGPoint, endYGPoint);
      	//out.print(yPoint);
      	double y = Double.parseDouble(yPoint);
      	
      	// Set the map envelope to center and zoom in on the geocoded point.
      	double scale = .00004;
      	double wWidth = scale*map.getWidth();
      	double wHeight = scale*map.getHeight();
      	//out.print(wWidth + " " +wHeight);
      	
      	double eLeft = x - (wWidth*.5);
      	double eRight = x + (wWidth*.5);
      	double eTop = y + (wHeight*.5);
      	double eBottom = y - (wHeight*.5);
      	Envelope env = new Envelope();
      	env.setMinX(eLeft) ;
      	env.setMinY(eBottom) ;
      	env.setMaxX(eRight) ;
      	env.setMaxY(eTop) ;
      	map.setEnvelope(env);
      	
      	// Draw the point on the acetate layer.
      	Point gPoint = new Point();
      	gPoint.setX(x);
      	gPoint.setY(y);
      	SimpleMarkerSymbol sm = new SimpleMarkerSymbol();
      	
      	sm.setColor("255,0,0");
      	sm.setWidth(15);
      	sm.setAntialiasing(true);
      	
      	sm.setMarkerType("star");
      	gPoint.setSymbol(sm);
      	
      	Acetate ao = new Acetate();
      	ao.setAcetateElement(gPoint);
      	ao.setUnits("database");
      	AcetateLayer al = new AcetateLayer("gpoint", null, null);
      	al.addAcetate(ao);
      	al.setVisible(true);
      	al.setName("AcetateLayer");
      	map.getLayers().add(al);
      	map.refresh();
      	mapImageUrl = map.getMapOutput().getURL();
      	//out.print(mapImageUrl);
      }
      
      %>
      
      <HTML>
      <HEAD>
      <TITLE>JSP Geocode (with RouteServer) Sample</TITLE>
      </HEAD>
      <BODY>
      
      <form action="geocodeRS.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="<%=gaddress%>">
      	              </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>
      
    2. Load the page as you would other JSP samples. For example:
      http://server_name/jspsamples/geocodeRS.jsp