Posted By

adrianparr on 09/05/11


Tagged

longitude distance latitude formula as3 haversine miles kilometres


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

tamak


AS3 Getting the Distance Between Two Geographical Points


 / Published in: ActionScript 3
 

URL: http://www.terrenceryan.com/blog/post.cfm/getting-the-distance-between-two-points-using-actionscript

Full credit for this goes to Terrence Ryan who converted the JavaScript version to ActionScript.

  1. private const RADIUS_OF_EARTH_IN_MILES:int = 3963;
  2. private const RADIUS_OF_EARTH_IN_FEET:int =20925525;
  3. private const RADIUS_OF_EARTH_IN_KM:int =6378;
  4. private const RADIUS_OF_EARTH_IN_M:int =6378000;
  5.  
  6. private function distanceBetweenCoordinates(lat1:Number,lon1:Number,
  7. lat2:Number,lon2:Number,
  8. units:String="miles"):Number{
  9.  
  10. var R:int = RADIUS_OF_EARTH_IN_MILES;
  11. if (units == "km"){
  12. R = RADIUS_OF_EARTH_IN_KM;
  13. }
  14. if (units == "meters"){
  15. R = RADIUS_OF_EARTH_IN_M;
  16. }
  17. if (units =="feet"){
  18. R= RADIUS_OF_EARTH_IN_FEET;
  19. }
  20.  
  21. var dLat:Number = (lat2-lat1) * Math.PI/180;
  22. var dLon:Number = (lon2-lon1) * Math.PI/180;
  23.  
  24. var lat1inRadians:Number = lat1 * Math.PI/180;
  25. var lat2inRadians:Number = lat2 * Math.PI/180;
  26.  
  27. var a:Number = Math.sin(dLat/2) * Math.sin(dLat/2) +
  28. Math.sin(dLon/2) * Math.sin(dLon/2) *
  29. Math.cos(lat1inRadians) * Math.cos(lat2inRadians);
  30. var c:Number = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  31. var d:Number = R * c;
  32.  
  33. return d;
  34. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: adrianparr on September 5, 2011

Note: I have also found this version here, http://labs.tomasino.org/tag/haversine/

You need to login to post a comment.