Thu 19 Feb 2009
Distance in CF — Mostly useless, but fun
Posted by JC under ColdFusion
No Comments
Assumption: table named locations that has a location name and the location's latitude & longitude as a single field, with lat and lon comma separated, both in signed degrees format.
This is an intentional cartesian join… so if you have a lot of locations, use with care.
<cfquery datasource="MyDSN" name="getDist">
SELECT DISTINCT
a.latlon AS thestart,
a.locName AS startname,
b.latlon AS theend,
b.locName AS endname
FROM locations a
CROSS JOIN locations b
WHERE a.locName<> b.locName
ORDER BY a.locName, b.locName
</cfquery>
<cfoutput query="getDist" group="startname">
<h4>#getDist.startname# to…</h4>
<ul><cfoutput>
<li>#getDist.endname# — #3963.0*acos(sin(listfirst(thestart)/57.295779513082323)*sin(listfirst(theend) / 57.295779513082323)+ cos(listfirst(thestart) / 57.295779513082323)*cos(listfirst(theend) / 57.295779513082323)*cos((listlast(theend) – listlast(thestart)) / 57.295779513082323))# Miles
</li>
</cfoutput>
</ul>
</cfoutput>
———————
This is based on a snippet of SQL code someone forwarded to me which was signed "RBarryYoung, 31-Jan-2009″ — so credit to RBarryYoung for it.
No Responses to “ Distance in CF — Mostly useless, but fun ”