 Find Code: All Words Any of the Words Exact Phrase Home   :   Code   :   Forums   :   Submit   :   Mailing List   :   About   :   Contact
 Code All VB.NET ASP.NET C# VB Classic ASP Classic Snippets Popular Resources Submit Code Forums Articles Tips Links Books Contest Link to us Calculate Distance and Radius in VB.NET Author: ZipCodeWorld E-mail: Click to e-mail author Website: http://www.zipcodeworld.com Submitted: 11/15/2005 Version: VB.NET 2002 Compatibility: VB.NET 2002, VB.NET 2003, VB.NET 2005 Category: Mathematics Views: 31564 This Visual Basic.NET function calculates the distance between two locations by using latitude and longitude from ZIP code, postal code or postcode. The result is available in miles, kilometers or nautical miles based on great circle distance calculation. Declarations: 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 'K' is kilometers (default) 'N' is nautical miles' United States ZIP Code/ Canadian Postal Code databases with latitude & longitude are available at http://www.zipcodeworld.com Code: Public Function distance(ByVal lat1 As Double, ByVal lon1 As Double, ByVal lat2 As Double, ByVal lon2 As Double, ByVal unit As Char) As Double Dim theta As Double = lon1 - lon2 Dim dist As Double = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta)) dist = Math.Acos(dist) dist = rad2deg(dist) dist = dist * 60 * 1.1515 If unit = "K" Then dist = dist * 1.609344 ElseIf unit = "N" Then dist = dist * 0.8684 End If Return dist End Function Private Function deg2rad(ByVal deg As Double) As Double Return (deg * Math.PI / 180.0) End Function Private Function rad2deg(ByVal rad As Double) As Double Return rad / Math.PI * 180.0 End Function