HOW TO
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.
Note: Proceed to the next step only after verifying that the samples can be run with the sample SantaClara map service.
<%@ 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>
http://server_name/jspsamples/geocodeRS.jsp
Get help from ArcGIS experts
Download the Esri Support App