Drawing circles on maps

I translated GeoTab's JS calc circle example into ColdFusion. We start with the lat and lng of an address. Then we take 1 degree steps around the circle. We could take 15 degree steps by changing the i++ to i = i + 15. Whatever number we use should go into 360 evenly to make it a closed circle.
Otherwise, we'd need to do some fancy-schmancy calculating to close the circle.

function generateCircleZonePoints(lat, lng, radius) {
   var earthRadius = 6371000;

   var latitude =  lat * ( pi()/180 ); // in radians
   var longitude =  lng * ( pi()/180 );
   var angularDistance = radius / earthRadius; // radius is the radius of the circle we are drawing in meters

   local.zonePoints = [];

   Math = createObject("java","java.lang.Math");
   // usage: Math.atan2(javacast("double",firstArg),javacast("double",secondArg));

   for (var i = 0; i <= 360; i++) {
      var radians = i * ( pi()/180 );
      var latitudeRadians = asin( sin( latitude ) * cos( angularDistance ) + cos( latitude ) * sin( angularDistance ) * cos( radians ) );
      var longitudeRadians = longitude + Math.atan2( javacast("double", sin( radians ) * sin( angularDistance ) * cos( latitude ) ), javacast("double", cos( angularDistance ) - sin( latitude ) * sin( latitudeRadians ) ) );

      arrayAppend( local.zonePoints, {
                  "x": toDegrees( longitudeRadians ),
                  "y": toDegrees( latitudeRadians )
               });
   }

   return local.zonePoints;
}

function toDegrees( radians ){
   return radians * ( 180/pi() );
}


Comments