Distance Calculation using Latitude and Longitude in VB.Net


ZIPCodeWorld provides this routine to calculate the distance between two points in VB.Net.

ZIPCodeWorld.com provides this routine to calculate the distance between two points (given the latitude/longitude of those points) in VB.Net. It is being used to calculate distance between two points lat1, long1 and lat2, long2 and uses radius of earth in kilometers or miles as an arguments using our ZIPCodeWorld(TM) and PostalCodeWorld(TM) products which offer the United States ZIP codes, Canadian Postal Codes, Mexican Postal Codes and North American Area Codes database subscription and solution services.



const pi = 3.14159265358979323846

Function distance(lat1, lon1, lat2, lon2, unit)
Dim theta, dist
theta = lon1 - lon2
dist = sin(deg2rad(lat1)) * sin(deg2rad(lat2)) + cos(deg2rad(lat1)) * cos(deg2rad(lat2)) * cos(deg2rad(theta))
dist = acos(dist)
dist = rad2deg(dist)
distance = dist * 60 * 1.1515
Select Case ucase(unit)
Case "K"
distance = distance * 1.609344
Case "N"
distance = distance * 0.8684
End Select
End Function


'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: this function get the arccos function from arctan function :::
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Function acos(rad)
If abs(rad) <> 1 Then
acos = pi/2 - atn(rad / sqr(1 - rad * rad))
ElseIf rad = -1 Then
acos = pi
End If
End function


'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: this function converts decimal degrees to radians :::
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Function deg2rad(deg)
deg2rad = cdbl(deg * pi / 180)
End Function

'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: this function converts radians to decimal degrees :::
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Function rad2deg(rad)
rad2deg = cdbl(rad * 180 / pi)
End Function

response.write distance(32.9697, -96.80322, 29.46786, -98.53506, "m") & " miles
"
response.write distance(32.9697, -96.80322, 29.46786, -98.53506, "k") & " kilometers
"
response.write distance(32.9697, -96.80322, 29.46786, -98.53506, "n") & " nautical miles
"





Reference: http://www.zipcodeworld.com/samples/distance.vb.html


Comments

Author: asdfg23 Mar 2010 Member Level: Gold   Points : 0

Thanks for sharing with us. Can you convert this code into C#

Author: hexahow24 Mar 2010 Member Level: Bronze   Points : 1

To get more information about Distance Calculation using Latitude and Longitude in C#, please refer to following URL:
http://www.dotnetspider.com/resources/37118-Distance-Calculation-using-Latitude-Longitude.aspx



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: