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

Discussion in 'Android Questions' started by Almora, Jul 30, 2019.

  1. Almora

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

    Code:
    ..........

    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
     
  2. DonManfred

    DonManfred Expert Licensed User

    Create a latlon for each point and use the same method.
    Code:
    Dim distance1 As Float = LocationA.DistanceTo(LocationB)
     
    mendiburen, MarkusR and Almora like this.
  3. Almora

    Almora Active Member Licensed User

    when I create it for each point, it only takes the first point and miscalculates.
     
  4. DonManfred

    DonManfred Expert Licensed User

    Hard to help with not seeing any code you are using
     
    mendiburen and emexes like this.
  5. Almora

    Almora Active Member Licensed User

    I try to measure the distance by clicking on two points on the map.

    Code:
    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
     
  6. emexes

    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:
    Code:
    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: Jul 31, 2019
    Shelby likes this.
  7. emexes

    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
     
    Shelby likes this.
  8. Almora

    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?
     
  9. emexes

    emexes Well-Known Member Licensed User

    Super.
    Even more super - presumably this is working ok (?)
    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:
    Code:
    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:
    Code:
    Dim DistanceAB As Float = pointa.DistanceTo(pointb)
    '''Dim DistanceBA As Float = pointb.DistanceTo(pointa)    'or in reverse direction, distance will(/should!) be the same
     
    Shelby likes this.
  10. DonManfred

    DonManfred Expert Licensed User

    He already got this answer in #2 of this thread.

    But he decided NOT TO give any Interest in it.
     
    mendiburen and Shelby like this.
  11. Almora

    Almora Active Member Licensed User

    someone is my own position. is calculating correctly. 121 mt

    calculates two different points incorrectly.
     

    Attached Files:

  12. DonManfred

    DonManfred Expert Licensed User

    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.
     
    mendiburen likes this.
  13. emexes

    emexes Well-Known Member Licensed User

    your answer was great, but...
    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.

    :)
     
    Shelby likes this.
  14. Almora

    Almora Active Member Licensed User

    Click on the map to select two points.

    Code:
    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: Jul 31, 2019
  15. emexes

    emexes Well-Known Member Licensed User

    <HUMOUR> Obviously this is not working out as well as I'd intended... :-/ </HUMOUR>
     
    Shelby likes this.
  16. emexes

    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?
     
    Shelby likes this.
  17. Almora

    Almora Active Member Licensed User

    I get two points by clicking on the map.

    Code:
    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
     
  18. emexes

    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?
     
    Shelby likes this.
  19. Almora

    Almora Active Member Licensed User

    Yes. That 's exactly what I want.
     
  20. emexes

    emexes Well-Known Member Licensed User

    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.
     
    Shelby likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice