Answered by:
Calculate Distance between 2 points
Question

Hi,
I used the haversine formula to calculate the distance between 2 points as susgegsted by severl people on this foum. When i compare the distance with the distance in bing maps or google maps, there is a difference.
I also tried the code posted at this link by R Brunditt.
Still i have the same problem. I want the distance in miles.
Here is my code:
Public Structure Position
Public Latitude As Double
Public Longitude As Double
End Structure
Public Sub haver()
Dim pos1 As New Position()
Dim pos2 As New Position()
'Dim hv As New Haversine()
Dim result As Double = Distance(pos1, pos2, DistanceType.Miles)
End Sub
Public Function Distance(ByVal pos1 As Position, ByVal pos2 As Position, ByVal type As DistanceType) As Double
Dim R As Double = If((type = DistanceType.Miles), 3960, 6371)
Dim dLat As Double = Me.toRadian(pos2.Latitude  pos1.Latitude)
Dim dLon As Double = Me.toRadian(pos2.Longitude  pos1.Longitude)
Dim a As Double = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(Me.toRadian(pos1.Latitude)) * Math.Cos(Me.toRadian(pos2.Latitude)) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2)
Dim c As Double = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)))
Dim d As Double = R * c
Return d
End Function
Private Function toRadian(ByVal val As Double) As Double
Return (Math.PI / 180) * val
End Function
Thanks in Advance
Monday, April 19, 2010 10:25 PM
Answers

But, there are several instances in the application where i do not ned routes at all. All i need is travel distance between 2 addresses.(Latitude and longitude for these 2 points are provided)
You will still have to use the directions service to get the distance, even if you do not display the routes. There is simply no other way to get "travel distance".
You've completely wasted your time using Haversine formula, which after all has only ever been an algorithm for calculating straight line distance ; of course that will almost always be less than travel distance.
About the only nonservices alternative is to accept inaccuracies and add say 40% to a Haversine calculation to get a very rough approximation.
 Marked as answer by rowter Wednesday, April 21, 2010 5:09 PM
Tuesday, April 20, 2010 5:52 PM
All replies

The Haversine formula calculates the straight line distance between two points. How are you measureing the distance using Bing or Google?If your using the routes, these will be different as these follow the roads. Also not that the Haversine is an approximate distance that is relative to a smooth sphere and as such does not take ground elevation into account.
Windows Live Developer MVP  http://rbrundritt.spaces.live.comMonday, April 19, 2010 11:26 PMModerator 
Also, you are not using Atan2 to calculate the central angle.
Windows Live Developer MVP  http://rbrundritt.spaces.live.comMonday, April 19, 2010 11:36 PMModerator 
I am using Bing Maps. I do not want a straight line distance between 2 points.
Is there any other method that i can use to get the distance between 2 points provided i have lat and long for the 2 points. (I do not want to use VEGetDirections method as i do not need to load map )
If i use Atan2 will it give accurate distance?
Thanks
Tuesday, April 20, 2010 12:34 AM 
I do not want a straight line distance between 2 points.
Tuesday, April 20, 2010 8:49 AM 
Use the routing web service.
http://msdn.microsoft.com/enus/library/cc981067(v=MSDN.10).aspx
Tuesday, April 20, 2010 1:51 PM 
Hi
Rossko 57,
These maps are for a transportation related service.
We do need routes for which i am using getdirections method provided by VE.(Using Bing Maps)
But, there are several instances in the application where i do not ned routes at all. All i need is travel distance between 2 addresses.(Latitude and longitude for these 2 points are provided)
This has to be saved. So, i want to avoid using Getdirections method as i do nto need to load the map or add pushpins in my scenario here.
Using haversine formula, for shorter distances the distance is going wrong by 0.5 to 1 mile. But for longer distances, the value i am getting is less than the value in google maps or bing maps by 150 miles.(Ex distance between NJ and Dallas)
So, i want to get the road travel distance between 2 points.(consuming minimum clicks or transactions).
Richard,
I tried Atan2 to cacluate the central angle. Still i see the difference.
Jonathan,
I have to look in to this Routing web sevice. Will try this now and see if it gets me what i need.
Thanks
Tuesday, April 20, 2010 3:31 PM 
But, there are several instances in the application where i do not ned routes at all. All i need is travel distance between 2 addresses.(Latitude and longitude for these 2 points are provided)
You will still have to use the directions service to get the distance, even if you do not display the routes. There is simply no other way to get "travel distance".
You've completely wasted your time using Haversine formula, which after all has only ever been an algorithm for calculating straight line distance ; of course that will almost always be less than travel distance.
About the only nonservices alternative is to accept inaccuracies and add say 40% to a Haversine calculation to get a very rough approximation.
 Marked as answer by rowter Wednesday, April 21, 2010 5:09 PM
Tuesday, April 20, 2010 5:52 PM 
You will still have to use the directions service to get the distance, even if you do not display the routes. There is simply no other way to get "travel distance".
Thanks. This is what i wanted to know in case haversine formula does not give accurate values.
Wednesday, April 21, 2010 5:09 PM 
Haversine formula does give accurate values (although your code of it happens to be flawed).
Haversine formula gives the "straight line" shortest distance over the globe.
Haversine formula can never provide travel distance by roads, and noone ever said it did. You chose the wrong tool, don't blame the tool.
Wednesday, April 21, 2010 5:27 PM 
You can get the travel distance by using the Bing Maps Routing Web Service. This will allow you to get the route information without having to render a route on a map.
Windows Live Developer MVP  http://rbrundritt.spaces.live.comWednesday, April 21, 2010 9:30 PMModerator