Calculate distance between two points

icycold68

Active member
Joined
Oct 16, 2012
Messages
30
Programming Experience
3-5
Hello

I am trying to calculate distance between two points using VB.NET and wondered if anyone could provide some assistance as to the best way to do this. Basically, I have created a stockists search site where I want users to type in their location using an available text box and the site will then locate stockists that are close to them e.g., within a 20 mile radius. The destination of the stockists will be pulled from a database. I need to firstly locate stockists that are close to the origin entered by the user and also display the distance to screen of that stockist.

I have seen this facility across many dating sites, such as POF.com, where you enter your location and people within a certain radius of where you live will be shown.

I am assuming this is a fairly difficult task to achieve?

Thanks in advance!
 
Exactly how will the points be defined? In a flat plane, distance between two points is calculated using trigonometry. Trig functions are found in the Math class in the .NET Framework.
 
The destination will be pulled from a database that contains an address for each available stockist. The origin will be specified by the user in a text box provided on screen where they will have the choice to enter a Post Code, City, Town or County. I need to be able to pull all stockists from the database that are close to the origin entered by the user e.g., if they enter their location as Peterborough, I need to retrieve all stockists that are either in Peterborough or close to this location. I then need to display the stockists in order of how far they are from the given origin.

Exactly how will the points be defined? In a flat plane, distance between two points is calculated using trigonometry. Trig functions are found in the Math class in the .NET Framework.
 
Thanks, but how do I actually get the latitude and longitude values for the origin/destination as I assume this function only accepts latitude/longitude values? Is there a function that can automatically work out a latitude/longitude value for a town/city/postcode?
 
I have now got to the stage where I have latitude and longitude values for all postcodes in the UK and am using the following function to try to calculate the distance between the origin and destination:


30Public Function distance(ByVal lat1 As Double, ByVal lon1 As Double, ByVal lat2 As Double, ByVal lon2 As Double, ByVal unit As Char) As Double

31 Dim theta As Double = lon1 - lon2

32 Dim dist As Double = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta))

33 dist = Math.Acos(dist)

34 dist = rad2deg(dist)

35 dist = dist * 60 * 1.1515

36 If unit = "K" Then

37 dist = dist * 1.609344

38 ElseIf unit = "N" Then

39 dist = dist * 0.8684

40 End If

41 Return dist

42End Function

43

44Private Function deg2rad(ByVal deg As Double) As Double

45 Return (deg * Math.PI / 180.0)

46End Function

47

48Private Function rad2deg(ByVal rad As Double) As Double

49 Return rad / Math.PI * 180.0

50End Function

The problem I have is that when I pass through some variable for lat1, lon1, lat2 and lon2 I keep getting the following error:

BC30455: Argument not specified for parameter 'lat1' of 'Public Function distance(lat1 As Double, lon1 As Double, lat2 As Double, lon2 As Double, unit As Char) As Double'.

I have declared the variable at the top of the page as follows:

Public lat1 As Double
Public lon1 As Double
Public lat2 As Double
Public lon2 As Double

And I have set the values of the variables using the following code:

conn.Open()

Dim strSQL2 As String

strSQL2 = "SELECT * FROM tbl_postcodes WHERE outcode = '" & txtPostcode.text & "'"

Dim myReader As MySql.data.MysqlClient.MySqlDataReader
Dim myCommand As New MySql.data.MysqlClient.MySqlCommand

myCommand.Connection = conn
myCommand.CommandText = strSQL2
myReader = myCommand.ExecuteReader

Do While myReader.Read()

lat1 = myReader("lat").ToDecimal()
lon1 = myReader("lng").ToDecimal()

lat2 = myReader("lat").ToDecimal()
lon2 = myReader("lng").ToDecimal()

Loop

myReader.Close()


Call distance

What am I doing wrong please?




Google Maps has an API that can be utilized. Other than that, you will have to add the lats and longs to your database manually.

A quick google showed me this

UK towns & cities list: All 43000 towns in England Scotland & Wales
 
Your Call Distance line should read similar to:

VB.NET:
TextBox1.text = distance(lat1, lon1, lat2, lon2, "K")


As you can see from the function header, it looks for some data in order for it to work..

VB.NET:
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

It is asking for the (lat1 Value, the lon1 Value) which is your start point, the (lat2 Value, the lon2 Value) which is your destination point and the Unit of measurement (Kilometres or Miles - represented by "K" or "N")

So for example if I plugged in the lat and long of:

29889Peter TavyDevonEnglandSX51772515007750050.57831-4.09874PL19 9Other

as the start point and the lat and long of:

29893PeterheadAberdeenshireScotlandNK134641350084650057.50850-1.77635AB42 1Town

as the destination point, I would get a return value of 735.525145311205 Kilometres

If you are doing the above correctly, then there must be something wrong in the reader section of your code..

I am not familiar with databases but it looks like you are only taking the "from" location and not the "to" location ?
 
Last edited:
Back
Top