<%-- 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 language="c#" %> <%@ Import Namespace="System" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Collections.Specialized" %> <%@ Import Namespace="System.Text" %> <%@ Import Namespace="OSGeo.MapGuide" %> Find Address <% String mgSessionId = GetRequestParameters()["SESSION"]; GeocodeAddress addr = null; bool success = false; try { // Initialize the web-extensions and connect to the Site using // the session identifier passed in the query string. InitializeWebTier(); MgUserInformation userInfo = new MgUserInformation(mgSessionId); MgSiteConnection siteConnection = new MgSiteConnection(); siteConnection.Open(userInfo); String address = GetRequestParameters()["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. Response.Write(""); Response.Write(""); Response.Write(""); Response.Write(""); Response.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(featureService, addressMarkerDataResId, mgSessionId, Response); } // 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(Convert.ToDouble(addr.lon), Convert.ToDouble(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 { Response.Write(""); } } catch (MgException e) { Response.Write(""); Response.Write(""); Response.Write(""); } %>
 Find Address
Search Results
"); Response.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.