Measurement of Distances

fraxinus

Member
Licensed User
Longtime User
Hi all,
I'm having a problem getting an accurate distance for my Lat/Long program.

I have used several methods of calculation which are set out below:

While they separately give the same resulting distance, they do not give the accurate distance as I require.
As an example:
Using WGS - 84 ellipsoid radius of 6378.137 Kms
and co-ordinates of:
Lat1: 51:37:42.66 N
Lon1: 3:55:44.09 W
Lat2: 51:23:51.51 N
Lon2: 1:18:3.61 W

The resulting distance using the methods below of :183.8513160623843 kms

While trying several Web sites using WGS -84
their results are 184.220....kms

Two of the methods I've used for calculations are basically,

Method 1:

Dim Lat1 As Double, Long1 As Double, Lat2 As Double, Long2 As Double
Dim EarthRadius, Angle, Distance As Double
EarthRadius = 6378.137 ' WGS -84 kilometers?

lat1 = txtlatdeg.Text
lat2 = txtlatdeg2.Text
long1 = txtlondeg.Text
long2 = txtlondeg2.Text

Angle = ACos(SinD(Lat1) * SinD(Lat2) + CosD(Lat1) * CosD(Lat2) * CosD(Long2 - Long1))
Distance = EarthRadius * Angle
txtdistance = Distance
...........................................................
Method 2:
Dim lat1 As Double, long1 As Double, lat2 As Double, long2 As Double
DEGREES_TO_RADIANS = (cPI / 180.0)
EARTH_RADIUS = 6378.137
lat1 =latt1.Text
lat2 = latt2.Text
long1 = lonn1.Text
long2 =lonn2.Text
rlat1 = DEGREES_TO_RADIANS * lat1
rlong1 = DEGREES_TO_RADIANS * long1
rlat2 = DEGREES_TO_RADIANS * lat2
rlong2 = DEGREES_TO_RADIANS * long2
p1 = Cos(rlat1) * Cos(rlong1) * Cos(rlat2) * Cos(rlong2)
p2 = Cos(rlat1) * Sin(rlong1) * Cos(rlat2) * Sin(rlong2)
p3 = Sin(rlat1) * Sin(rlat2)
ret = p1 + p2 + p3
If ret = 1 Then Return 0
ret = ACos(ret)
ret = (ret * EARTH_RADIUS)
Result.Text = ret
Return ret
.......................................

Any help will be appreciated:
Thanks
Rob
 

klaus

Expert
Licensed User
Longtime User
B4X:
Dim Latitude1, Latitude2, Longitude1, Longitude2, Distance As Double
Dim Location1, Location2 As Location

Location1.Latitude = Latitude1
Location1.Longitude = Longitude1
Location2.Latitude = Latitude2
Location2.Longitude = Longitude2
Distance = Location1.DistanceTo(Location2)
Best regards.
 
Upvote 0

SiriusDG

Member
Licensed User
Longtime User
Units for Distance

Can someone please let me know what the units are for GetDistance? In some cases feet/yards are good, but in many, miles would be better. In either case, I am not sure yet. Thanx.

David
 
Upvote 0

latch

Active Member
Licensed User
Longtime User
I retract my question, but I'll leave it here in case someone else is a dumb as I. I was save the lat&long in a file but the variable I was using was a 'long'. So everything to the right of my decimal was being truncated.

I am getting 117497 from this code:

B4X:
distance = oldlocation.DistanceTo(Location1)

Both locations are the same- anyone else seen this?
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Without GPS permission for Location, for ref:
B4X:
'meters distance
Sub DistanceCalc (p1 As GeoPoint, p2 As GeoPoint) As Long
'This sub calculates the distance and course between two Lat/Lon coordinates.
'The formulas are based on this site: http://williams.best.vwh.net/avform.htm (Ed Williams)
Dim lat1, lat2, lon1, lon2, Dist, d As Double
lat1=p1.Latitude
lon1=p1.Longitude
lat2=p2.Latitude
lon2=p2.Longitude
    Try
        lat1 = lat1 * cPI / 180
        lon1 = -lon1 * cPI / 180
        lat2 = lat2 * cPI / 180
        lon2 = -lon2 * cPI / 180
        d = 2 * ASin(Sqrt(Power(Sin((lat1-lat2)/2),2) + Cos(lat1)*Cos(lat2)*Power(Sin((lon1-lon2)/2),2)))
        Dist = d * 180 * 60 * 1852/cPI
'        If Cos(lat1) < 1e-7 Then
'            If (lat1 > 0) Then
'                tc1 = cPI
'            Else
'                tc1= 2*cPI
'            End If
'        Else
'            sn = Sin(lon2-lon1)
'            If Abs(sn) < 1e-7 Then
'                If lat1 > lat2 Then tc1 = cPI Else tc1 = 2*cPI
'            Else If sn < 0 Then      
'                tc1=ACos((Sin(lat2)-Sin(lat1)*Cos(d))/(Sin(d)*Cos(lat1)))  
'            Else    
'                tc1=2*cPI-ACos((Sin(lat2)-Sin(lat1)*Cos(d))/(Sin(d)*Cos(lat1)))
'            End If
'        End If
'        'calculating decimal X,Y
'        Course = tc1 * 180 / cPI
'        'x=Dist* Cos(tc1-cPI/2)
'        'y=Dist* Sin(tc1-cPI/2)
        Return Abs(Dist)
    Catch
'        Dist = 0
'        Course = 0
        Return 0
    End Try
End Sub

Course calcualtion is disabled now, but also is workable.
 
Upvote 0

IslamQabel

Active Member
Licensed User
Longtime User
Dear klaus..

In your code....i think that you should write
Latitude1= Location1.Latitude..to store the obtained result into Latitude1 ............what do you think?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I suppose you are refering to the code in post #4.
This code calculates the distance between two locations given by their coordinates Latitude1, Longitude1 and Latitude2, Longitude2 !
And the code uses the DistanceTo function of the Location object from the GPS library.
To do this we need to define the two Location1 and Location2 objects and set their properties.
This is the answer to post #1.
If you already know the two locations as Location objects you can use DistanceTo directly !
 
Upvote 0

IslamQabel

Active Member
Licensed User
Longtime User
I already understand the code and understand the functions also......but what i am asking .....
Latitude1, Latitude2, Longitude1, Longitude2, Distance....are declared variables and have no value,....and to get the values of longitude,latitude ...you should get them from the Location object members.....to store or get the value ...
Longitude1= Location1.Longitude.....storing the value of Longitude of location1 in the variable "Longitude1" variable....
but according to your code.....you get the longitude value from the variable...understand me?
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
In many cases geo information are dynamic and in real time.. they are not fetched from a database or stored variables.
So I would go with Klaus solution.
After using them then one can save them in a database if they are that important.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
The code in post #4 is just an example on how to use DistanceTo with the Dim declarations of the variables.
Of course, the user needs somewhere inbetween to give Latitude1, Longitude1 and Latitude2, Longitude2 values.
As already mentioned, in the example Latitude1, Longitude1 and Latitude2, Longitude2 are supposed having values given somewhere and Location1 and Location2 are not known.
 
Upvote 0
Top