Hi Charlie
Thanks for your assistance and now the code to detect the distance between 2 points using long / lat works a treat Some background for the use of the code for those who are interested Its being used to detect when a tracking device is within x distance from a nominated point. Its for geo fence alerting, in order words we take a co ordinate long / lat (which you can visualise as being the centre of a circle with a radius of x distance). The coordinates of the centre of the circle are set, which creates the gep fence. The variable coordinates are the location of the mobile tracking device. Each time the tracking device reports its current location (coordinates) those coordinates are passed through the code to determine how far the reported location is from the centre of the coordinates representing the centre of the circle. When the distance between the 2 points is less than the radius then by definition it means the portable device is now inside the geo fence circle area if the distance is greater than the radius then the reported location is outside the geo fence circle You then create processes to run eg cfmail alerts for emails dependant on the business rules you need to set relating to when the circle has been entered / existed <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 '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> <cfquery datasource="#ds#" username="#un#" password="#pw#" NAME="GeoFences"> select latitude, longitude, fence_name, fence_status from GeoFences where deviceid = '#url.deviceid# </cfquery> <cfloop query="GeoFences"> <cfif #radius# gt #getDistance(url,gps_latit , url.gps_longit , latitude, longitude, 'kilometers')#> <cfset status='Entered'> <cfelse> <cfset status='Exited'> </cfif> <!--- Buisiness Rules --- > <cfif #status# neq #fence_status#> Process business rule given the geo fence status calculated is different to the previous status recorded </cfif> <cfquery datasource="#ds#" username="#un#" password="#pw#" NAME="GeoFences"> update GeoFences Set fence_status='#status#’ where deviceid = '#url.deviceid#' </cfquery> </cfloop> NB: TrackingCentral is now a registered product & services provider for the National Disability Insurance Scheme, under the category of Assisted Technology Regards Claude Raiola Director TrackingCentral Pty. Ltd Free Call 1300 255 990 From: cfaussie@googlegroups.com [mailto:cfaussie@googlegroups.com] On Behalf Of Charlie Arehart Sent: Thursday, 26 March 2015 2:12 PM To: cfaussie@googlegroups.com Subject: RE: [cfaussie] Help With Dynamic Naming A CfScript Function Claude, I’m not picking a fight but I don’t see how that answers my question at all. The good news is that I think you are very close to a solution, but you seem to have missed what I said. Why do you have the function being defined within this query loop you refer to (in words, but don’t reflect in the code). Are you saying, perhaps, that some of the variables within the function are references to query variables? I don’t see that. It seems the UDF (user-defined function) is self-contained, and it operates on whatever is passed into it. So I’ll ask again: why don’t you simply put this *definition* of the function OUTSIDE the loop and just *call the function from within the loop*, passing it the values within the loop on which you want to perform these calculations? And your subject and initial question refer to this being about “dynamic naming” of the function, but your code below shows no such thing. When you said in your first note that you were trying to do that with “function getDistance#id#”, I wonder: were you trying that simply because you were getting the error about not being able to duplicate the function? Again, you don’t need to have the function being duplicated. Just move the function out of the loop and you should be all set. :-) /charlie From: cfaussie@googlegroups.com <mailto:cfaussie@googlegroups.com> [mailto:cfaussie@googlegroups.com] On Behalf Of rai...@ozemail.com.au <mailto:rai...@ozemail.com.au> Sent: Wednesday, March 25, 2015 11:49 PM To: cfaussie@googlegroups.com <mailto:cfaussie@googlegroups.com> Subject: RE: [cfaussie] Help With Dynamic Naming A CfScript Function This is the code I am using /** * Calculates distance between Latitude/Longitude points using haversine formula. * * @param lat1 latitude of first point (Required) * @param lon1 longitude of first point (Required) * @param lat2 latitude of second point (Required) * @param lon2 longitude of second point (Required) * @param units Units for return value. Default is miles. (Optional) * @return Returns numeric distance between the two points. Units varies, default is miles. * @author Henry Ho (henryho...@gmail.com <mailto:henryho...@gmail.com> ) * @version 0, January 8, 2013 */ function getDistance(lat1, lon1, lat2, lon2, units = 'miles') { // earth's radius. Default is miles. var radius = 3959; if (arguments.units EQ 'kilometers' ) radius = 6371; else if (arguments.units EQ 'feet') radius = 20903520; var toRad = pi() / 180; var dLat = (lat2-lat1) * toRad; var dLon = (lon2-lon1) * toRad; var a = sin(dLat/2)^2 + cos(lat1 * toRad) * cos(lat2 * toRad) * sin(dLon/2)^2; var c = 2 * createObject("java","java.lang.Math").atan2(sqr(a), sqr(1-a)); return radius * c; } Its wrapped inside a cfloop as I need to validate code received into our portal from mobile devices against data stored in the database hence the need to look around the data records from the query as often there will be more than 1 records to test against NB: TrackingCentral is now a registered product & services provider for the National Disability Insurance Scheme, under the category of Assisted Technology Regards Claude Raiola Director TrackingCentral Pty. Ltd Free Call 1300 255 990 From: cfaussie@googlegroups.com <mailto:cfaussie@googlegroups.com> [mailto:cfaussie@googlegroups.com] On Behalf Of Charlie Arehart Sent: Thursday, 26 March 2015 1:26 PM To: cfaussie@googlegroups.com <mailto:cfaussie@googlegroups.com> Subject: RE: [cfaussie] Help With Dynamic Naming A CfScript Function Right, but why do you need the name of the function to be dynamic? Why isn’t it enough that you pass in args within the loop (and declare the function outside of it)? /charlie From: cfaussie@googlegroups.com <mailto:cfaussie@googlegroups.com> [mailto:cfaussie@googlegroups.com] On Behalf Of rai...@ozemail.com.au <mailto:rai...@ozemail.com.au> Sent: Wednesday, March 25, 2015 11:14 PM To: cfaussie@googlegroups.com <mailto:cfaussie@googlegroups.com> Subject: RE: [cfaussie] Help With Dynamic Naming A CfScript Function I am calculating distance between 2 sets of lon / lat and the information is dynamic so I need to capture the values of the dynamic data and compare them to the values stored in a database table of which there may been to be several records tested against the values in the dynamic data Eg 1 set of co ordinate received from a device in the field data is received by the listener and needs to be tested against co ordinates recorded in the database table of which there may be several records needing to be tested against hence the need for the loop Regards Claude Raiola -- You received this message because you are subscribed to the Google Groups "cfaussie" group. To unsubscribe from this group and stop receiving emails from it, send an email to cfaussie+unsubscr...@googlegroups.com <mailto:cfaussie+unsubscr...@googlegroups.com> . To post to this group, send email to cfaussie@googlegroups.com <mailto:cfaussie@googlegroups.com> . Visit this group at http://groups.google.com/group/cfaussie. For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "cfaussie" group. To unsubscribe from this group and stop receiving emails from it, send an email to cfaussie+unsubscr...@googlegroups.com <mailto:cfaussie+unsubscr...@googlegroups.com> . To post to this group, send email to cfaussie@googlegroups.com <mailto:cfaussie@googlegroups.com> . Visit this group at http://groups.google.com/group/cfaussie. For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "cfaussie" group. To unsubscribe from this group and stop receiving emails from it, send an email to cfaussie+unsubscr...@googlegroups.com <mailto:cfaussie+unsubscr...@googlegroups.com> . To post to this group, send email to cfaussie@googlegroups.com <mailto:cfaussie@googlegroups.com> . Visit this group at http://groups.google.com/group/cfaussie. For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "cfaussie" group. To unsubscribe from this group and stop receiving emails from it, send an email to cfaussie+unsubscr...@googlegroups.com. To post to this group, send email to cfaussie@googlegroups.com. Visit this group at http://groups.google.com/group/cfaussie. For more options, visit https://groups.google.com/d/optout.