Today, for work, my boss asked for me to create a bit of functionality that would allow users to return lists of people from a database who live within [x] miles of an entered zip code.  I've never created something like this before, but I've been around long enough to know that this kind of functionality requires gigantic databases of zip codes, trigonometric calculations, etc. simply to return the distance between two zip codes.

Fortunately, others have created such things and have kindly sydicated them as webservices.  Webservices are very simple–they are a collection of functions that are remotely accessible to developers.  So, if you want to create a search for Amazon books on your website, you hook up to Amazon's book-syndication service, call the appropriate method (like "getBooks()" or something) and use whichever programming language you is using to parse out the information in a usable fashion.  Here's the one I'm using:

 

http://webservices.imacination.com/distance/Distance.jws?wsdl

ColdFusion makes stuff like this ridiculously easy within its "cfinvoke" tag.  In about 4 lines of code, you call the method, pass in the method's require arguments, and create a variable for usage later on.  

<cfinvoke webservice="http://webservices.imacination.com/distance/Distance.jws?wsdl" method="getDistance" returnvariable="distance">

The zip code webservice required two arguments, "fromZip" and "toZip".  So, I created a form with a field for the user to enter their zip code and desired radius.  On the submission of the form, I loop over the webservice, passing into it the current zip code value of the recordset's row.

<cfinvokeargument name="fromZip" value="#FORM.fromZip#">
<cfinvokeargument name="toZip" value="#rsZips.cityName[i]#">

The next part was a bit tricky for me.  If the variable returned from the webservice is not somehow made unique, its value will be continually overwritten with each iteration of the the loop.  So, to solve this problem, I simply created a structure in which I could store the variable, as well as the relevant person's information related to that distance.  

<cfset masterStruct = structNew()>………

…….<cfset agentDetails = structNew()>
<cfset agentDetails.name = #rsZips.agentName[i]#>
<cfset agentDetails.zip = #rsZips.cityName[i]#>
<cfset agentDetails.distance = #distance#>

<cfset structInsert(masterStruct, '#i#', #agentDetails#)>

After completing the loop, I finish it all up by looping over the structure of structures that I have created, accessing the desired values of the current structure if its distance was within the value determined by the user.

<cfloop index="i" from="1" to="#rsZips.recordCount#">
    <cfif #masterStruct[i].distance# LT #radius#>
        <cfoutput>#removeChars(masterStruct[i].distance, 5, 20)# – #masterStruct[i].name#<br /><br /></cfoutput>
    </cfif>
</cfloop>

Pretty simple.  ColdFusion definitely makes my life easy as it provides straightforward and intuitive solutions in no time flat.

To see a demo of this, click here .  (P.S.  All of the people's zip's in the dbase are around Lexington, KY, so I would try "40504").