1

I have some geofence script that I am using on a cfm page:

<cfscript> 
 function getDistance(loclat, loclon, fencelat, fencelon, units = 'kilometers')
 { 
    // earth's radius. Default is miles.
    var radius = 3959;
    if (arguments.units EQ 'kilometers' )
        radius = 6371;
    else if (arguments.units EQ 'meters')
        radius = 6371393;
    else if (arguments.units EQ 'feet')
        radius = 20903520;

    var toRad = pi() / 180;
    var dLat = (fencelat-loclat) * toRad;
    var dLon = (fencelon-loclon) * toRad; 
    var a = sin(dLat/2)^2 + cos(loclat * toRad) * cos(fencelat * toRad) * sin(dLon/2)^2; 
    var c = 2 * createObject("java","java.lang.Math").atan2(sqr(a), sqr(1-a));

    return radius * c;

 }
</cfscript>

I then query my geofence table and the output lists for those records where geofences have been set for a particular device. When the geofence query returns multiple records, the cfoutput loops those records and as the code within the cfoutput calls the js function I get an error advising that the function has been called 2 times in the one template.

I "assume" the solution is to place the script function itself inside the cfoutput and make the function name unique by appending the recordid for each to the function name. Is this the correct solution?

If it is, my question is how can I dynamically append the record id to the function name within the cfscript? For example:

function getDistance#recid#(loclat, loclon, fencelat, fencelon, units = 'kilometers')

Then when I call for the function value, output

<cfif #fence_meter_radius# gt #getDistance(loclat, loclon, latitude, longitude, 'kilometers')#>

I welcome any assistance with the above.

Leigh
  • 28,765
  • 10
  • 55
  • 103
  • are all those elements written in the javascript transferable as cf code ???, I would not consider myself as an advanced cf coder –  Feb 01 '17 at 01:35
  • 1
    By enclosing the function in `cfscript` tags it already *is* being treated as CF code. Whether or not it is valid CF code is a different question. Is your function supposed to be javascript or CF? – Leigh Feb 01 '17 at 02:10
  • How did this one work out? – James A Mohler Mar 21 '19 at 16:00

0 Answers0