<%-- -Copyright (C) 2004-2006 Autodesk, Inc. -This library is free software; you can redistribute it and/or -modify it under the terms of version 2.1 of the GNU Lesser -General Public License as published by the Free Software Foundation. -This library is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -Lesser General Public License for more details. -You should have received a copy of the GNU Lesser General Public -License along with this library; if not, write to the Free Software -Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA --%> <%@ page import="org.osgeo.mapguide.*" %> <%@ page import="java.util.*" %> <%@ page import="java.io.*" %> <%@ page import="java.text.*" %> <%@ page import="javax.servlet.jsp.*" %> <%@ page import="javax.servlet.http.*" %> <%@ include file ="../utilityfunctions.jsp" %> <%@ include file ="findaddressfunctions.jsp" %> Find Address <% String mgSessionId = request.getParameter("SESSION"); GeocodeAddress addr = null; boolean success = false; try { // Initialize the Web Extensions and connect to the Tux Site using // the session identifier stored in JSP session state. InitializeWebTier(); MgUserInformation userInfo = new MgUserInformation(mgSessionId); MgSiteConnection siteConnection = new MgSiteConnection(); siteConnection.Open(userInfo); String address = request.getParameter("address") + ", Sheboygan, WI"; // Make the request to geocoder.us passing the address. addr = requestGeocodeAddress(address); if (addr != null && addr.lon.length() > 0 && addr.lat.length() > 0) { // The geocode successfully returned a location. out.write(""); out.write(""); out.write(""); out.write(""); out.write(""); // Create ResourceService and FeatureService objects and use // the ResourceService object to open the map instance in // session state. MgResourceService resourceService = (MgResourceService)siteConnection.CreateService(MgServiceType.ResourceService); MgFeatureService featureService = (MgFeatureService)siteConnection.CreateService(MgServiceType.FeatureService); MgMap map = new MgMap(siteConnection); map.Open("Sheboygan"); // Check the map for the AddressMarker layer. If it does not // exist then create a feature source to store address results // and a layer to display them. MgResourceIdentifier addressMarkerDataResId = new MgResourceIdentifier("Session:" + mgSessionId + "//AddressMarker.FeatureSource"); MgLayer addressLayer = GetLayerByName(map, "AddressMarker"); if (addressLayer == null) { CreateAddressMarkerFeatureSource(featureService, addressMarkerDataResId); addressLayer = CreateAddressMarkerLayer(resourceService, addressMarkerDataResId, mgSessionId); map.GetLayers().Insert(0, addressLayer); } else if (addressLayer.GetVisible()) { // If the layer exists and is visible, then display the // previous results. EmitAddressResults(out, featureService, addressMarkerDataResId, mgSessionId); } // Insert the results of the Geo-Code into the temporary // feature source and ensure the address marker layer // is visible. MgAgfReaderWriter geometryReaderWriter = new MgAgfReaderWriter(); MgGeometryFactory geometryFactory = new MgGeometryFactory(); MgPoint addrPoint = geometryFactory.CreatePoint(geometryFactory.CreateCoordinateXY(Double.parseDouble(addr.lon), Double.parseDouble(addr.lat))); MgPropertyCollection properties = new MgPropertyCollection(); properties.Add(new MgStringProperty("Address", addr.address1)); properties.Add(new MgGeometryProperty("Location", geometryReaderWriter.Write(addrPoint))); MgFeatureCommandCollection commands = new MgFeatureCommandCollection(); commands.Add(new MgInsertFeatures("AddressMarker", properties)); featureService.UpdateFeatures(addressMarkerDataResId, commands, false); addressLayer.SetVisible(true); addressLayer.ForceRefresh(); map.Save(); success = true; } else { out.write(""); } } catch (MgException e) { out.write(""); out.write(""); out.write(""); } %>
 Find Address
Search Results
"); out.write(" " + addr.address1 + "
" + addr.address2 + "
Longitude: " + addr.lon + "
Latitude: " + addr.lat + "
Address not found: " + address + "
" + e.GetExceptionMessage()+ "
" + e.GetDetails() + "
Geocoding Provided by: geocoder.us.
Locative Tecnologies offers the free geocoder.us web service for non commercial purposes. If you wish to use this geocoding web service in an production application you must sign up for a commercial license. Click the link above for more information.