Return to Snippet

Revision: 50830
at September 5, 2011 05:31 by adrianparr


Initial Code
private const RADIUS_OF_EARTH_IN_MILES:int = 3963;
private const RADIUS_OF_EARTH_IN_FEET:int =20925525;
private const RADIUS_OF_EARTH_IN_KM:int =6378;
private const RADIUS_OF_EARTH_IN_M:int =6378000;

private function distanceBetweenCoordinates(lat1:Number,lon1:Number,
                                            lat2:Number,lon2:Number,
                                            units:String="miles"):Number{

	var R:int = RADIUS_OF_EARTH_IN_MILES;
	if (units == "km"){
		R = RADIUS_OF_EARTH_IN_KM;
	}
	if (units == "meters"){
		R = RADIUS_OF_EARTH_IN_M;
	}
	if (units =="feet"){
		R= RADIUS_OF_EARTH_IN_FEET;
	}

	var dLat:Number = (lat2-lat1) * Math.PI/180;
	var dLon:Number = (lon2-lon1) * Math.PI/180;

	var lat1inRadians:Number = lat1 * Math.PI/180;
	var lat2inRadians:Number = lat2 * Math.PI/180;

	var a:Number = Math.sin(dLat/2) * Math.sin(dLat/2) + 
                       Math.sin(dLon/2) * Math.sin(dLon/2) * 
                       Math.cos(lat1inRadians) * Math.cos(lat2inRadians);
        var c:Number = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
        var d:Number = R * c;

	return d;
}

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

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

Initial Title
AS3 Getting the Distance Between Two Geographical Points

Initial Tags

                                

Initial Language
ActionScript 3