Link to home
Start Free TrialLog in
Avatar of JArndt42
JArndt42Flag for United States of America

asked on

Calculate Mileage from Lat Lon

I have the following snippet of code that gets the lat and lon from a starting point and ending point. It works like a charm. However the mileages are off when compared to any map program, web based or not, such as PC miler. And when I say off I mean by as much as 100 miles off, usually less than what PC miler calculates. Any ideas would be very helpful.
Function GetMiles() As Long
Dim db As Database
Dim rsFrom As Recordset
Dim rsTo As Recordset
Dim DistanceLat As Double
Dim DistanceLong As Double
Dim Distance As Long

Set db = CurrentDb
Set rsFrom = db.OpenRecordset("select zipcode,latitude,longitude from ZipCodes where zipcode= """ & txtStartZip & """")
Set rsTo = db.OpenRecordset("select zipcode,latitude,longitude from ZipCodes where zipcode= """ & txtZip & """")
If rsFrom.RecordCount > 0 Then
    DistanceLat = (rsTo.Fields("Latitude") - rsFrom.Fields("Latitude")) * 69.1
    DistanceLong = (69.1 * (rsTo.Fields("Longitude") - rsFrom.Fields("Longitude")) * (Cos(rsFrom.Fields("Longitude") / 57.3)))
    Distance = (((DistanceLat ^ 2) + (DistanceLong ^ 2)) ^ 0.5)
    GetMiles = Distance
Else
    MsgBox "Not Working"
End If

End Function

Open in new window


for an testing purposes here are the beginning lat and lon for:
Start: Lat: 44.062231  Lon:  -123.168041

End: Lat  37.671778   Lon:  -121.012493
Avatar of mbizup
mbizup
Flag of Kazakhstan image

What are your expected results for your sample data, and what are you actually getting?
Avatar of JArndt42

ASKER

the result is 448 miles. If you go to google or pc miler and enter 97402 to 95350 zip codes you get 541 miles. those are the lats and lons for both those zip locations.
ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
so it is. I guess I will have to find a different way of finding my miles huh? Any recommendations?
What exactly are you interested in for your results?

 Road mileage (maps)?

Or a more pure distance taking into account things like earth curvature as opposed to a straight line, but not road-based limitations?
This picture should give you an idea of just how different some results can be...
http://www.distancefromto.net/
I am more interested in road miles as this will be used to calculate fuel usage and cost per mile driven. I have a function i have used in the past that would open Google Maps run the route and then another function that would look at the source code of the results page and key on the mileage. However Google changes their source code ever so slightly every once in a while and I have to go find the difference and change the string my function is looking for. So although that works it is a pain in the butt because I have to send the updated function to all the users after I spend all the time to find what the difference is.
Okay - I don't think this is really an Access question so much as an algorithms/math/science/etc question.

You made your issue very clear to me, but I don't have the specific topic expertise to help with it - and I think you would have better results by posting a question with the explanation in your last comment, including the following topics (zones):

Algorithms
Miscellaneous
Math/Science

Maybe include the Access Zone for help with the VBA if needed.  Or post in the more academic zones first to nail down the process/algorithm and then repost here for help with the code.
Well I really do not have the time or resources to pursue this issue any further. Thank you for bringing the issue to my attention.