Android Example [Coordinates] Distance Features

Alexander Stolte

Expert
Licensed User
I added a new feature to my apps yesterday, this feature show you as text how far you are from the other coordinate.

This looks so:

within 1 kilometer: very close
More than 1 kilometer: near
More than 5 kilometers: far
More than 10 kilometers: 10KM, if 15 kilometers then 15KM

So, now we need the GPS library.

and then the code:

B4X:
'We initialize first the 2 Location. The first Location is our location and the second location is the target location.

    Dim l1, l2 As Location
    l1.Initialize2(MyCurrentLatitude, MyCurrentLongitude)
    l2.Initialize2(TargetCoordinateLatitude), TargetCoordinateLongitude)
   
'now we need the distance between our location and the target location

    Dim distance As Long
    distance =    l1.DistanceTo(l2)  'the result is in meter
   
'because the distance is in meter we must specify it in thousandths, like miliseconds in seconds

    If distance < 1000 Then
        lbl_distance.Text = "very close!"
    Else If distance > 1000 Then
        lbl_distance.Text = "near!"
    Else If distance > 5000 Then
        lbl_distance.Text = "far!"
    Else If distance > 10000 Then

       'if the target more than 10 kilometers away, then we want to show it in kilometers, because we have the value in meters, we are convert it to kilometer.

        lbl_distance.Text = Round(distance/1000) & "KM"
    End If
and that is all.

i hope it helps you.
 
Top