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

Almora

Active Member
Licensed 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
 

Almora

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

Almora

Active Member
Licensed 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)
     points.Add(pointa)
    GoogleMap1.AddMarker(Point1.Latitude, Point1.Longitude, "("&Point1.Latitude&", "&Point1.Longitude&")")
    
    pointb.Initialize(GoogleMap1.MyLocation.Latitude, GoogleMap1.MyLocation.Longitude)
    points.Add(pointb)
    GoogleMap1.AddMarker(Point1.Latitude, Point1.Longitude, "("&Point1.Latitude&", "&Point1.Longitude&")")

    lat1=pointa.Latitude
    lon1=pointa.Longitude
    
    lat2=pointb.Latitude
    lon2=pointb.Longitude
    
 
End Sub
 

emexes

Well-Known Member
Licensed User
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

Well-Known Member
Licensed User
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:

upload_2019-7-31_12-45-56.png
 

Almora

Active Member
Licensed 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

Well-Known Member
Licensed User
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(GoogleMap1.MyLocation.Latitude, GoogleMap1.MyLocation.Longitude)    'can set pointb to be anywhere, doesn't have to be your current location
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
 

emexes

Well-Known Member
Licensed User
He already got this answer in #2 of this thread. But he decided NOT TO give any Interest in it.
your answer was great, but...
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
Licensed User
Click on the map to select two points.

B4X:
Sub MapFragment1_Click (Point1 As LatLng)
 
 
     pointa.Initialize(Point1.Latitude, Point1.Longitude)
     points.Add(pointa)
    GoogleMap1.AddMarker(Point1.Latitude, Point1.Longitude, "("&Point1.Latitude&", "&Point1.Longitude&")")
 
    pointb.Initialize(GoogleMap1.MyLocation.Latitude, GoogleMap1.MyLocation.Longitude)
    points.Add(pointb)
    GoogleMap1.AddMarker(Point1.Latitude, Point1.Longitude, "("&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

Well-Known Member
Licensed User
Ok... you wish to measure the distance between two points.

One of the points is Point1.

What is the other point?
 

Almora

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

B4X:
Sub MapFragment1_Click (Point1 As LatLng)
 
 
     pointa.Initialize(Point1.Latitude, Point1.Longitude)
     points.Add(pointa)
    GoogleMap1.AddMarker(Point1.Latitude, Point1.Longitude, "("&Point1.Latitude&", "&Point1.Longitude&")")
 
    pointb.Initialize(GoogleMap1.MyLocation.Latitude, GoogleMap1.MyLocation.Longitude)
    points.Add(pointb)
    GoogleMap1.AddMarker(Point1.Latitude, Point1.Longitude, "("&Point1.Latitude&", "&Point1.Longitude&")")

    lat1=pointa.Latitude
    lon1=pointa.Longitude
 
    lat2=pointb.Latitude
    lon2=pointb.Longitude
 
 
End Sub
 

emexes

Well-Known Member
Licensed User
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?
 

emexes

Well-Known Member
Licensed User
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.
 
Top