# 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``````

#### emexes

##### Expert
So your click-to-mark mechanism looks like it's working, but it's not.

No worries, we've all been tricked by our code occasionally.

#### emexes

##### Expert
This is a bit of a crude fix, in that every time you click the map it will add another marker, gradually filling up your screen, but... hey, at least it'll get you pointed in the right direction:
B4X:
``````Sub MapFragment1_Click (Point1 As LatLng)

'add to some global/persistent list of points ???
pointa.Initialize(Point1.Latitude, Point1.Longitude)

GoogleMap1.AddMarker(Point1.Latitude, Point1.Longitude, "(" & Point1.Latitude & ", " & Point1.Longitude & ")")

lat1 = lat2
lon1 = lon2

lat2 = Point1.Latitude
lon2 = Point1.Longitude

End Sub``````
Give that a burl, let us know how you go.

Longtime User
ok.
I will try

#### klaus

##### Expert
Longtime User
I would do it like this:
Add the locations to the list.
B4X:
``````Public lstLocations As List
lstLocations.Initialize

Sub MapFragment1_Click (Point As LatLng)
'add the location to the List
Private Location1 As Location
Location1.Initialize2(Point.Latitude, Point.Longitude)

GoogleMap1.AddMarker(Point1.Latitude, Point1.Longitude, "(" & Point1.Latitude & ", " & Point1.Longitude & ")")
End Sub``````

Then you can go through the locations to calculate the distance:
B4X:
``````Private Sub CalculateDistance(LocationList As List) As Double
Private Distance As Double
For i = 0 To LocationList.Size - 2
Private Location1, Location2 As Location
Location1 = LocationList.Get(i)
Location2 = LocationList.Get(i + 1)
Distance = Distance + Location1.DistanceTo(Location2)
Next
Return Distance
End Sub``````

#### emexes

##### Expert
I would do it like this:
+1, assuming that a never-shrinking set of points is ok.

TBH I was wondering why the calculation is done on a button click, and not just done automatically when a marker is added to the screen. That would mean that the total path/perimeter distance could just be accumulated into one total, no need to recalculate everything each time.

But it wasn't super-clear what the use-case was, indeed we weren't even sure what the problem was to begin with, and having just got our heads above water regarding this and the reason it was happening, I was happy to take a breather before taking another dive.

Replies
6
Views
1K
Replies
3
Views
784
Android Question GPS app distance calculation
Replies
1
Views
549
Replies
4
Views
1K
Replies
1
Views
2K