Android Question how do I calculate the distance between two different points?

Almora

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

on google maps
how do I calculate between two different points?
thnks..

B4X:
..........

line=GoogleMap1.AddPolyline
        points.Initialize
 
        line.color=Colors.Red
        line.Geodesic=True
        line.Visible =True
        line.Width=5
     pointt.Initialize(GoogleMap1.MyLocation.Latitude, GoogleMap1.MyLocation.Longitude)
         points.Add(pointt)
        lat1=GoogleMap1.MyLocation.Latitude
        lon1=GoogleMap1.MyLocation.Longitude


......


..MapFragment1_Click (Point1 As LatLng)

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

GoogleMap1.AddMarker(Point1.Latitude, Point1.Longitude, "("&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
Licensed User
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.

:)
 
Upvote 0

emexes

Expert
Licensed User
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)
    points.Add(pointa)  

    'add marker to map
    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.
 
Upvote 0

klaus

Expert
Licensed User
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)
    lstLocations.Add(Location1)

    'add marker to map
    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
 
Upvote 0

emexes

Expert
Licensed User
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.
 
Upvote 0
Top