VISUAL STUDIO USAGE TUTORIAL
This page is a brief run-through of one way to consume our GIS web services. In this case, we will
be using Microsoft Visual Studio 2005 or 2008. The tutorial assumes you have basic familiarity with Visual
Studio, ASP.NET AJAX, and ASP.NET web design in general. Create a new project (Web Site, Web
Application, Windows Application, etc.) and follow the steps below.
1. Add a Web Reference
Right-click on the root node of your project and select:
Add ASP.NET Folder »
App_WebReferences.
Then right-click on the new App_WebReferences folder and select
Add Web Reference....
The Add Web Reference wizard will pop up. Point the wizard to
lookup the URL http://www.rfocus.com/ws/GeoCoder.asmx
by entering the web site in the URL textbox and clicking the Go button.
The wizard should detect the GeoCoder web service and display some information about the service.
It will allow you to choose a Web reference name. In our examples, we chose
RFocusWS. Once you click the
Add Web Reference button, Visual Studio will automatically generate a proxy class that
you can use to consume the web service in your project.
2a. Consume the Web Service Directly
If you simply wish to access the web services in your code, you may do so by calling the methods directly:
GeoCoder geocoder = new GeoCoder();
GeocodeResult result = geocoder.IPAddressLookup("172.0.0.1");
2b. Create a Local Proxy Web Service
Or, you can extend the web services for further use in your application, e.g. ASP.NET AJAX. This
next step involves setting up a local proxy web service so that we can tie in some AJAX code with
the GIS web services. First, right click on the root node of your project and select Add New
Item... and choose Web Service. In our example, it's called
GeoCoderProxy.asmx. Make sure the Place code
in separate file box is checked. This will create a stub web service file in the root directory,
which points to the new class file in the App_Code directory.
Next we need to open a "tunnel" so our JavaScript code can reach the web services. In
App_Code/GeoCoderProxy.cs, you can see three of these
tunnels that will allow the AJAX code to access the web service residing on the www.rfocus.com server.
You can name the methods anything you want, really. In this case, we've opted to use the ASP.NET AJAX
library to return a JSON string, so the names of the methods reflect this. Even though the methods have
GeocodeResult as the return type, this will
be automatically parsed into a JavaScript-readable JSON string by ASP.NET.
3. Client-side Code
You can see the source code for the three examples of HTML pages that use ASP.NET AJAX to consume the
www.rfocus.com web services via our local proxy web service:
These implementation examples harness the Google Maps API
to visually demonstrate a practical use of the web services. Each of these examples contains a
ScriptManager tag with a reference to the local proxy web service:
<asp:ScriptManager runat="server">
<Services>
<asp:ServiceReference Path="GeoCoderProxy.asmx" />
</Services>
</asp:ScriptManager>
This generates a JavaScript object named GeoCoderProxy, which allows us to execute such code as:
<script type="text/javascript">
function getPoint()
{
GeoCoderProxy.PhysicalAddressLookupJson("Madrid, Spain", showPointData);
}
function showPointData(geocodeResult)
{
alert("Latitude: " + geocodeResult.Point.Latitude + "\n" +
Longitude: " + geocodeResult.Point.Longitude);
}
</script>
In this case, we're passing a string ("Madrid, Spain") to the PhysicalAddressLookupJson()
method of our local proxy web service, and telling the AJAX framework to call the showPointData()
JavaScript method when the web service result is returned. We can then parse the result, which will
be a JavaScript object mirroring the structure of the GeocodeResult C# class.