# Location.Distanceto Example Help

#### fraxinus

##### Member
Longtime User
Could A.K.S show some examples of the Location.Distanceto and how accurate is it. I have Racing Pigeons and would like to develop an app to give accurate distances between two given Lat/Long coordinates.
thanks
Rob

#### derez

##### Expert
Longtime User
Here is an example:

B4X:
``````Sub GPS_LocationChanged (Location1 As Location)
If location1.AccuracyValid Then gps_red_timer.Enabled = False Else gps_red_timer.Enabled = True
point.Lat = Location1.Latitude
point.lon =  Location1.Longitude
speed = location1.Speed * gps_speed_to_kmh  ' =3.6
If speed > 1 Then track = location1.Bearing
If flag.tgt = 1 Then
bearing = location1.BearingTo(tgt_location)
range = location1.DistanceTo(tgt_location)
End If``````

The question of accuracy is almost not related to the calculation, but to the accuracy of the GPS positioning at the time of run. It depends on the number of satellites and the relative geometry between them. If the GPS on both pigeons are with good accuracy you'll get an RMS error of 20-50 meter.

#### fraxinus

##### Member
Longtime User
Sorry David, but I was after examples of code to measure the distance of 2 fixed lat/long positions. i.e
Distance between Lat1:51:37:42.66 N, Lon1: 3:55:44.09 W and Lat2: 51:23:51.55 N Lon2: 1:18:3.61 W

I've used several examples of the Great Circle method (see one below)but the results are not accurate.

Sub CalcDistance_click
Dim Lat1 As Double, Long1 As Double, Lat2 As Double, Long2 As Double
Dim EarthRadius, Angle, Distance As Double
EarthRadius = 6378.167 'WGS - 84
Angle = ACos(SinD(Lat1) * SinD(Lat2) + CosD(Lat1) * CosD(Lat2) * CosD(Long2 - Long1))
Distance = EarthRadius * Angle
Return Distance
End Sub

#### Erel

##### B4X founder
Staff member
Longtime User
Here is an example:
B4X:
``````    Dim loc1, loc2 As Location
loc1.Initialize2("36.5", "30.123")
loc2.Initialize2("40", "40")
Log(loc1.DistanceTo(loc2))
Log(loc1.BearingTo(loc2))``````
This is a native method and the documentation doesn't mention its accuracy.

#### klaus

##### Expert
Longtime User
Hi fraxinus.

Erel's solution is what you are looking for.
I tried your example from this thread Measurement of distance.
The resut is 184.220... .
DistanceTo is probably calculating with the Vincenty's formula

Best regards.

#### fraxinus

##### Member
Longtime User
Thanks Erel and Klaus for your invaluable help.
The code is very close too what I require at the moment.
I've tried all of the various Great Circle formulae. Haversine, Spherical Law of Cosines,etc also using various Earth Radius from the mean radius of 6,371km to
WGS - 84 Ellipsoid of:sign0098: 6378.167 kms . All have given basically the same resulting distances, but were also out by a few hundred meters in my coding.
Could you tell me what Earth radius is used in the example Erel posted, and could it be altered?
Many thanks all

Rob

#### klaus

##### Expert
Longtime User
The code Erel proposed is the calculation provided by the Android GPS location class.
As I wrote in my prvious post, I suppose that it uses the Vincenty's formulas. Googling around for Vincenty's formulas I found a site with a distance calculator with these formulas and found the result that you were expecting. These equations are much more 'sophisticated' than the 'Grand circle' formulas I have tested like you with the same results as you, they all give the same result. I too, tried with different Earth radiuses, but that's not the difference we expect.

Best regards.

#### fraxinus

##### Member
Longtime User
Hi Kaus
Yes, I tried out Vincenty's formulae and it matches the Calculation provided by Android GPS location class. He uses GRS-80 Ellipsoid radius of 6378.137 m
You have been extremely helpful.
Thanks All
Rob

#### ogransjo

##### New Member
Longtime User
Is it possible to put this in a Service Module ?

Here is an example:

B4X:
``````Sub GPS_LocationChanged (Location1 As Location)
If location1.AccuracyValid Then gps_red_timer.Enabled = False Else gps_red_timer.Enabled = True
point.Lat = Location1.Latitude
point.lon =  Location1.Longitude
speed = location1.Speed * gps_speed_to_kmh  ' =3.6
If speed > 1 Then track = location1.Bearing
If flag.tgt = 1 Then
bearing = location1.BearingTo(tgt_location)
range = location1.DistanceTo(tgt_location)
End If``````

The question of accuracy is almost not related to the calculation, but to the accuracy of the GPS positioning at the time of run. It depends on the number of satellites and the relative geometry between them. If the GPS on both pigeons are with good accuracy you'll get an RMS error of 20-50 meter.

I want to have this in a Service Module. Is it possible? I need a example :BangHead:

Staff member
Longtime User

#### netchicken

##### Active Member
Longtime User
OK, I feel stupid, but I am just not getting this location.distanceto calculation.

location.latitude and location.longitude are obvious, how do you get the location.to() calculation when you can only store lat and lon?

Sure its seems to be current location - stored location, but as location is divided into 2 components how does it work?

Also, what is speed measured in? Meters per second?

Last edited:

#### Erel

##### B4X founder
Staff member
Longtime User
location.latitude and location.longitude are obvious, how do you get the location.to() calculation when you can only store lat and lon?
Location.DistanceTo returns the distance in meters between two Location objects. The current one and the one passed as a parameter.
Also, what is speed measured in? Meters per second?
Basic4android Search: location.speed

#### netchicken

##### Active Member
Longtime User
Thank Erel.

I really had a brain futz there. I got it working OK

Replies
21
Views
5K
Android Tutorial Background location tracking
Replies
15
Views
42K
Replies
9
Views
2K
Replies
23
Views
135K
Replies
5
Views
2K