# Android Questionhow do I calculate the distance between two different points?

#### Almora

##### Active Member
Longtime User
hi..
I calculate the distance between my own location and another location.

how do I calculate between two different points?
thnks..

B4X:
``````..........

points.Initialize

line.color=Colors.Red
line.Geodesic=True
line.Visible =True
line.Width=5

......

..MapFragment1_Click (Point1 As LatLng)

pointt.Initialize(Point1.Latitude, Point1.Longitude)

lat2=pointt.Latitude
lon2=pointt.Longitude

........

Sub Button1_Click
line.points=points
LocationA.Initialize
LocationA.Latitude = lat1
LocationA.Longitude = lon1

LocationB.Initialize
LocationB.Latitude =  lat2
LocationB.Longitude = lon2

Dim distance1 As Float = LocationA.DistanceTo(LocationB)
Label1.Text=distance1
End Sub``````

#### DonManfred

##### Expert
Longtime User
how do I calculate between two different points?
Create a latlon for each point and use the same method.
B4X:
``Dim distance1 As Float = LocationA.DistanceTo(LocationB)``

#### Almora

##### Active Member
Longtime User
when I create it for each point, it only takes the first point and miscalculates.

#### Almora

##### Active Member
Longtime User
I try to measure the distance by clicking on two points on the map.

B4X:
``````Sub MapFragment1_Click (Point1 As LatLng)

pointa.Initialize(Point1.Latitude, Point1.Longitude)

lat1=pointa.Latitude
lon1=pointa.Longitude

lat2=pointb.Latitude
lon2=pointb.Longitude

End Sub``````

#### emexes

##### Expert
edit: DonManfred's way is better, but if you like to see how things are done, consider this:

For "short" distances under eg 100 kilometres, just convert the degrees to metres, and use pythagoras. Circumference of earth is nominally 40,000 kilometres = 40,000,000 metres.

Eg:
B4X:
``````Dim YMetres As Float = (Lat2 - Lat1) * 40000000 / 360    'each degree latitude is 111 km
Dim XMetres As Float = (Lon2 - Lon1) * 40000000 / 360 * CosD((Lat1 + Lat2) / 2)    'longitude degrees get smaller as you move away from equator

Dim DistanceMetres As Float = Sqrt(XMetres * XMetres + YMetres * YMetres)``````
If you need something more accurate (once you get past the precise size of the earth, vs local variation), go to the master:

https://edwilliams.org/avform.htm#Dist

Last edited:

#### emexes

##### Expert
Just realised there is a .DistanceTo function in the GPS library too, so depending on which libraries you have already included in your project, that might be more convenient to use:

#### Almora

##### Active Member
Longtime User
I'm using distanceTo.

I calculate the distance between my location and another location.

do I calculate between two different points on a map?

#### emexes

##### Expert
I'm using distanceTo.
Super.
I calculate the distance between my location and another location.
Even more super - presumably this is working ok (?)
do I calculate between two different points on a map?
If you need to, then yes.

I am confused by that question. I think it lost something in translation.

You can use the .DistanceTo method with any LatLng or Location object/variable, ie, it doesn't have to be the LatLon or Location object/variable that you received from the GPS via GoogleMap.

So if you have two map locations, eg:
B4X:
``````pointa.Initialize(Point1.Latitude, Point1.Longitude)
pointb.Initialize(Point2.Latitude, Point2.Longitude)``````
then you can get the distance between pointa and pointb by doing:
B4X:
``````Dim DistanceAB As Float = pointa.DistanceTo(pointb)
'''Dim DistanceBA As Float = pointb.DistanceTo(pointa)    'or in reverse direction, distance will(/should!) be the same``````

#### Almora

##### Active Member
Longtime User
someone is my own position. is calculating correctly. 121 mt

calculates two different points incorrectly.

#### Attachments

• SmartSelectImage_2019-07-31-07-07-32.png
78.3 KB · Views: 262
• SmartSelectImage_2019-07-31-07-12-17.png
83.9 KB · Views: 253

#### DonManfred

##### Expert
Longtime User
calculates two different points incorrectly.
WHERE is the code used? Upload a small project to show the issue.
OR post all relevant code!

You are doing something wrong i guess.

#### emexes

##### Expert
He already got this answer in #2 of this thread. But he decided NOT TO give any Interest in it.
I think it lost something in translation.
probably at their end, so I gave them the benefit of the doubt and expanded your answer (thus saving your time for more complicated questions) so that hopefully some of it might translate and be understood by the OP.

#### Almora

##### Active Member
Longtime User
Click on the map to select two points.

B4X:
``````Sub MapFragment1_Click (Point1 As LatLng)

pointa.Initialize(Point1.Latitude, Point1.Longitude)

lat1=pointa.Latitude
lon1=pointa.Longitude

lat2=pointb.Latitude
lon2=pointb.Longitude

End Sub

Sub Button1_Click
line.points=points
LocationA.Initialize
LocationA.Latitude = lat1
LocationA.Longitude = lon1

LocationB.Initialize
LocationB.Latitude =  lat2
LocationB.Longitude = lon2

Dim distance1 As Float = LocationA.DistanceTo(LocationB)
Label1.Text=distance1
End Sub``````

Last edited:

#### emexes

##### Expert
thus saving your time for more complicated questions
<HUMOUR> Obviously this is not working out as well as I'd intended... :-/ </HUMOUR>

#### emexes

##### Expert
Ok... you wish to measure the distance between two points.

One of the points is Point1.

What is the other point?

#### Almora

##### Active Member
Longtime User
I get two points by clicking on the map.

B4X:
``````Sub MapFragment1_Click (Point1 As LatLng)

pointa.Initialize(Point1.Latitude, Point1.Longitude)

lat1=pointa.Latitude
lon1=pointa.Longitude

lat2=pointb.Latitude
lon2=pointb.Longitude

End Sub``````

#### emexes

##### Expert
I'll take a punt and assume that you want to click on the map twice.

The first click adds a point/marker to the map.
The second click adds another point/marker to the map.
And then clicking Button1 calculates and displays the distance between those two points.

Is this what you want to do?

#### Almora

##### Active Member
Longtime User
Yes. That 's exactly what I want.

#### emexes

##### Expert
I get two points by clicking on the map.
If by points you mean markers, then: I agree, that is precisely what your code will do. It will add two markers, both located at Point1 ie looks like just one marker.

And if you click on the map again, I suspect it will add another two markers, wherever you clicked the second time.

Replies
2
Views
890
Replies
6
Views
2K
Replies
4
Views
3K
Replies
22
Views
8K