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:
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: content, bgcolor: initial !important, align: left"] [/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]30[/TD]
[TD="class: content, bgcolor: initial !important, align: left"]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[/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]31[/TD]
[TD="class: content, bgcolor: initial !important, align: left"] Dim theta As Double = lon1 - lon2[/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]32[/TD]
[TD="class: content, bgcolor: initial !important, align: left"] Dim dist As Double = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta))[/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]33[/TD]
[TD="class: content, bgcolor: initial !important, align: left"] dist = Math.Acos(dist)[/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]34[/TD]
[TD="class: content, bgcolor: initial !important, align: left"] dist = rad2deg(dist)[/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]35[/TD]
[TD="class: content, bgcolor: initial !important, align: left"] dist = dist * 60 * 1.1515[/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]36[/TD]
[TD="class: content, bgcolor: initial !important, align: left"] If unit = "K" Then[/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]37[/TD]
[TD="class: content, bgcolor: initial !important, align: left"] dist = dist * 1.609344[/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]38[/TD]
[TD="class: content, bgcolor: initial !important, align: left"] ElseIf unit = "N" Then[/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]39[/TD]
[TD="class: content, bgcolor: initial !important, align: left"] dist = dist * 0.8684[/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]40[/TD]
[TD="class: content, bgcolor: initial !important, align: left"] End If[/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]41[/TD]
[TD="class: content, bgcolor: initial !important, align: left"] Return dist[/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]42[/TD]
[TD="class: content, bgcolor: initial !important, align: left"]End Function[/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]43[/TD]
[TD="class: content, bgcolor: initial !important, align: left"] [/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]44[/TD]
[TD="class: content, bgcolor: initial !important, align: left"]Private Function deg2rad(ByVal deg As Double) As Double[/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]45[/TD]
[TD="class: content, bgcolor: initial !important, align: left"] Return (deg * Math.PI / 180.0)[/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]46[/TD]
[TD="class: content, bgcolor: initial !important, align: left"]End Function[/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]47[/TD]
[TD="class: content, bgcolor: initial !important, align: left"] [/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]48[/TD]
[TD="class: content, bgcolor: initial !important, align: left"]Private Function rad2deg(ByVal rad As Double) As Double[/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]49[/TD]
[TD="class: content, bgcolor: initial !important, align: left"] Return rad / Math.PI * 180.0[/TD]
[/TR]
[/TABLE]
[TABLE="width: 0"]
[TR="bgcolor: initial !important"]
[TD="class: number, bgcolor: initial !important, align: left"]50[/TD]
[TD="class: content, bgcolor: initial !important, align: left"]End Function
[/TD]
[/TR]
[/TABLE]
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