Calling a JavaScript function within a loop

597 Views Asked by At

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.

0

There are 0 best solutions below