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
 

DonManfred

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

Almora

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

Almora

Active Member
Licensed User
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)
     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
 
Upvote 0

emexes

Expert
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:
Upvote 0

emexes

Expert
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
 
Upvote 0

Almora

Active Member
Licensed User
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?
 
Upvote 0

emexes

Expert
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
 
Upvote 0

Almora

Active Member
Licensed User
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
    SmartSelectImage_2019-07-31-07-07-32.png
    78.3 KB · Views: 256
  • SmartSelectImage_2019-07-31-07-12-17.png
    SmartSelectImage_2019-07-31-07-12-17.png
    83.9 KB · Views: 250
Upvote 0

emexes

Expert
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.

:)
 
Upvote 0

Almora

Active Member
Licensed User
Longtime 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:
Upvote 0

Almora

Active Member
Licensed User
Longtime 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
 
Upvote 0

emexes

Expert
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?
 
Upvote 0

emexes

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