Geocoding with Google Map API and ColdFusion

What is geocoding?

Geocoding is the process of finding associated geographic coordinates ( latitude and longitude) from other geographic data, such as street addresses, or ZIP codes (postal codes).

Note: Reverse geocoding is the process of converting geographic coordinates into a human-readable address.

Requirement of geocoding in Web Application:

  1. Geocoding allows the quick plotting of the address on a map .

  2. To locate the nearest service center by taking the customer address as input.

  3. Used in internet advertising[ to deliver targeted advertising banners based on the location of the website visitor’s IP address. ]

  4. To provide better search results depending upon the location.

  5. In a particular company’s website, to implement functionalites like “Find a Nearest Store”,by which a user can locate the nearest store.

Available Geocoding APIs:

There are several geocoding APIs available in the market.Some of the frequently used free APIs are:

  1. Bing Maps from Mircosoft

  2. Google Map API

  3. Cloudmade Geocoding

  4. Data Science Toolkit

  5. MapQuest Geocoding API

  6. Yahoo PlaceFinder API

In terms of speed,efficiency and correctness Bing Map and Google Map APIs are considered to be the best one as they cover most of the places in the world.

In this Blog, I will give an example of Integrating Google Map API with ColdFusion to find the driving distance between any two physical addresses.

Key Points to remember while using Google Map API:

  1. The google geocoding API has certain usage limits like for normal account it is 2,500 requests per day. While for business customers it is 100,000 requests.For more information please visit:

  2. All the addresses must be in a proper format which can be easily recognized by google map API. Suggested format [Full Street Name,City,State,Zipcode]

Finding distance between two places using ColdFusion and Google Map API:

Steps to Find distance between any two physical addresses.

  1. Find the Latitude and Longitude of the two places. [Using Google Geocoding API]

  2. Find the driving distance between the two coordinates.[Using Google Distance Matrix API]

Step 1:

Find the Latitude and Longitude of the two places.

This is a simple function which will take the user address as input and will return the lattitude and longitude in a structure.

<cffunction name="findLatLong"  output="false" returntype="struct">
        <cfargument name="street" type="string" required="true" >
        <cfargument name="city" type="string" required="true">
        <cfargument name="state" type="string" required="true">
        <cfargument name="zip" type="string" required="true">
<!--- Replacing all the spaces with '+' and removing ',' --->
        <cfset VARIABLES.loc_street=trim(replacenocase(replacenocase(ARGUMENTS.street," ","+","all"),",","","all"))>
        <cfset VARIABLES.loc_city=trim(replacenocase(replacenocase(," ","+","all"),",","","all"))>
        <cfset VARIABLES.loc_state=trim(replacenocase(replacenocase(ARGUMENTS.state," ","+","all"),",","","all"))>
        <cfset VARIABLES.loc_zip=trim(replacenocase(replacenocase(," ","+","all"),",","","all"))>
        <!---Creating an address string in proper format for google maps(Streetname,City,State,Zipcode) --->
        <cfset VARIABLES.urladdress="#loc_street#,#loc_city#,#loc_state#,#loc_zip#">
            <cfhttp result="geocode" url="" method="get">
                <cfhttpparam type="header" name="Content-Type" value="text/xml">
            <cfset VARIABLES.gcode = "#xmlparse(geocode.filecontent)#">
            <cfif VARIABLES.gcode.geocoderesponse.status.xmltext EQ "OK">
                <cfset VARIABLES.newlat = "">
                <cfset VARIABLES.newlon = "#VARIABLES.gcode.geocoderesponse.result.geometry.location.lng.xmltext#">
                <cfset VARIABLES.gcodefail = 0>
                <cfset VARIABLES.newlat = "0">
                <cfset VARIABLES.newlon = "0">
                <cfset VARIABLES.gcodefail = 1>
                <cfdump var="#cfcatch#">
                <cfset VARIABLES.newlat = "0">
                <cfset VARIABLES.newlon = "0">
                <cfset VARIABLES.gcodefail = 1>
        <cfset VARIABLES.locationstruct=structNew()>
        <cfset VARIABLES.locationstruct.latitude=newlat>
        <cfset VARIABLES.locationstruct.longitude=newlon>
        <cfset VARIABLES.locationstruct.gcodefail=gcodefail>
        <cfreturn VARIABLES.locationstruct>

Step 2:

Find the driving distance between the two coordinates.

This function will take the latitude and longitude of two places and will return the driving distance and time required in a structure format.

<cffunction name="googleGeoCodeify" access="remote" output="true" returntype="struct">
   <cfargument name="location1_lat" type="numeric" required="true">
   <cfargument name="location1_long" type="numeric" required="true">
   <cfargument name="location2_lat" type="numeric" required="true">
   <cfargument name="location2_long" type="numeric" required="true">
            <cfset VARIABLES.drivingdetails = structnew()>
            <!--- build querystring to send to google --->
            <cfset VARIABLES.googlequerystring="origins=#ARGUMENTS.location1_lat#,#ARGUMENTS.location1_long#">
            <cfset VARIABLES.googlequerystring=googlequerystring & "&destinations=#ARGUMENTS.location2_lat#,#ARGUMENTS.location2_long#">
            <cfset VARIABLES.googlequerystring=left(googlequerystring,len(googlequerystring)-1)>
            <cfset VARIABLES.googlequerystring=googlequerystring & "&mode=driving&language=en&avoid=tolls&units=imperial&sensor=false">
            <cfset VARIABLES.googleurl="">
            <cfhttp url="#VARIABLES.googleurl#" method="GET" throwonerror="no" timeout="10"/>
            <cfset VARIABLES.resultstr = deserializeJSON(cfhttp.FileContent)>
            <cfif VARIABLES.resultstr.status eq "OK">
                <cfset VARIABLES.drivingdetails.drivingdistance = REReplace(VARIABLES.resultstr.rows[1].elements[1].distance.text,"[^0-9.]?","","all")>
                <cfset VARIABLES.drivingdetails.drivingtime = REReplace(VARIABLES.resultstr.rows[1].elements[1].duration.text,"[^0-9.]?","","all")>
                <cfset VARIABLES.drivingdetails.drivingdistance = "-999999">
                <cfset VARIABLES.drivingdetails.drivingtime = "-999999">
                <cfset VARIABLES.drivingdetails.drivingdistance = "-999999">
                <cfset VARIABLES.drivingdetails.drivingtime = "-999999">
                <cfdump var="#cfcatch#">
        <!--- return the struct with requrired driving distance and time --->
        <cfreturn VARIABLES.drivingdetails>


<cfset source = findLatLong('300 Boylston Ave E', 'Seattle', 'WA', '98102')>
<cfset destination = findLatLong('555 N 105th St','Seattle','WA','98133')>
<cfset distance = googleGeoCodeify(source.latitude,source.longitude,destination.latitude,destination.longitude)>
<cfdump var="#distance#">