/*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ /*:: :*/ /*:: This routine calculates the distance between two points (given the :*/ /*:: latitude/longitude of those points). It is being used to calculate :*/ /*:: the distance between two zip codes or postal codes. :*/ /*:: :*/ /*:: Latitudes are positive and longitudes are negative for :*/ /*:: points north of the Equator and west of England. :*/ /*:: :*/ /*:: Passed to function: :*/ /*:: lat1, lon1 = latitude and longitude of point 1 (in decimal degrees) :*/ /*:: lat2, lon2 = latitude and longitude of point 2 (in decimal degrees) :*/ /*:: unit = the unit you desire for results :*/ /*:: where: 'M' is statute miles (default) :*/ /*:: 'K' is kilometers :*/ /*:: 'N' is nautical miles :*/ /*:: :*/ /*:: United States zip code and Canadian postal code databases with :*/ /*:: latitude and longitude are available at :*/ /*:: http://www.unitedstateszipcodes.org/zip-code-database/ :*/ /*:: :*/ /*:: A good explanation of longitude and latitude can be found at :*/ /*:: http://www.satsig.net/lat_long.htm :*/ /*:: :*/ /*:: Modified php formula from http://www.phpbuilder.com/snippet/ :*/ /*:: detail.php?type=snippet&id=1184 :*/ /*:: :*/ /*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ FUNCTION Distance(xLat1, xLong1, xLat2, xLong2, cUnit) LOCAL nLat1, nLong1, nLat2, nLong2, nTheta, nDist, nMiles nLat1:=IIF(VALTYPE(xLat1)="C", VAL(ALLTRIM(xLat1)), xLat1) nLong1:=IIF(VALTYPE(xLong1)="C", VAL(ALLTRIM(xLong1)), xLong1) nLat2:=IIF(VALTYPE(xLat2)="C", VAL(ALLTRIM(xLat2)), xLat2) nLong2:=IIF(VALTYPE(xLong2)="C", VAL(ALLTRIM(xLong2)), xLong2) DEFAULT cUnit:="M" // trigonometry functions are from XBTools3 nTheta:= nLong1 - nLong2 nDist := SIN(DTOR(nLat1)) * SIN(DTOR(nLat2)) + ; COS(DTOR(nLat1)) * COS(DTOR(nLat2)) * COS(DTOR(nTheta)) // DTOR=degrees to radians nDist := ACOS(nDist) nDist := RTOD(nDist) // RTOD=radians to degrees nMiles:= nDist * 60 * 1.1515 IF cUnit=="K" // kilometers RETURN(nMiles * 1.609344) ELSEIF cUnit=="N" // nautical miles RETURN(nMiles * 0.8684) ELSEIF cUnit=="M" // statute miles RETURN(nMiles) ENDIF RETURN(0) *****************************************************************************