Android Question Issue with DistanceTo

Keith Flanagan

Member
Licensed User
Hi All

I think there is a problem with the DistanceTo method in Location.

When I run the following code I get a return value of 7482515 for something that is only 52ish meters away.

Any ideas what may be wrong here? Thanks

B4X:
    Dim BigBenLongitude As Double = 51.500770
    Dim BigBenLatitude As Double = -0.124622
       
    Dim TescoLat As Double = 51.501137
    Dim TescoLong As Double = -0.125092

   
    Dim L1, L2 As Location
    L1.Initialize2(TescoLat, TescoLong)
    L2.Initialize2(BigBenLatitude, BigBenLongitude)
   
    Dim DistanceInMeters As Long = L1.DistanceTo(L2)
   
    ToastMessageShow("Distance: " & CRLF & DistanceInMeters,True) '7482515

See Google maps showing this:
1588447019258.png


Version Details:
1588447844632.png
 
Last edited:

Jorge M A

Well-Known Member
Licensed User
Upvote 0

drgottjr

Well-Known Member
Licensed User
Longtime User
i get 66.1 meters.

here's the deal:
1) you NEED to read the documentation relating to the Location object
2) prepare moist towel for some head banging and blood sopping
3) you need to feed location2() 2 strings, not 2 variables declared as strings

specifically:
B4X:
   ' works:
    L1.Initialize2("-0.124622","51.500770" )     ' you can use 3 formats: degrees, minutes or seconds
    L2.Initialize2("-0.125092", "51.501137")    ' you can use 3 formats: degrees, minutes or seconds
    
    ' she no work:
    L1.Initialize2(BigBenLatitude,BigBenLongitude )     ' no, no no
    L2.Initialize2(TescoLat, TescoLong)                          ' no, no, no

why, you ask? good question.
 
Upvote 0

Keith Flanagan

Member
Licensed User
Thanks drgottjr

It turns out I had swapped the values for Log and Lat for Big Ben in my first code.

Below is the code with the variables the correct way and now I get 52, which is as close to google maps.

B4X:
    Dim BigBenLatitude As Double = 51.500770
    Dim BigBenLongitude As Double = -0.124622
        
    Dim TescoLat As Double = 51.501137
    Dim TescoLong As Double = -0.125092
    
    Dim BigBenLoc, TescoLoc As Location
    TescoLoc.Initialize2(TescoLat, TescoLong)
    BigBenLoc.Initialize2(BigBenLatitude, BigBenLongitude)
'   
    Dim DistanceInMetersFromBigBen As Long = BigBenLoc.DistanceTo(TescoLoc)
    ToastMessageShow("Distance: " & CRLF & DistanceInMetersFromBigBen,True) '52
 
Upvote 0

Jorge M A

Well-Known Member
Licensed User
Just for additional information:

you need to feed location2() 2 strings,
why, you ask?

That's because the formats that the object accepts:
Values can be formatted in any of the three formats:
Degrees: [+-]DDD.DDDDD
Minutes: [+-]DDD:MM.MMMMM (Minute = 1 / 60 of a degree)
Seconds: [+-]DDD:MM:SS.SSSSS (Second = 1 / 3600 of a degree)
Example:

Dim L1 As Location
L1.Initialize2("45:30:30", "45:20:15")
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Maybe a road was closed for works and had to take a diversion 😂
 
Upvote 0

drgottjr

Well-Known Member
Licensed User
Longtime User
Just for additional information:
That's because the formats that the object accepts:

sorry, you misunderstood the question. the format supplied was one of the accepted formats. the question was related to why passing a value as a string variable gave a different answer than when the same value as a literal string was passed.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Just to clarify.
The problem was not the variable !
But the inversion between latitude and longitude values !!!
As already reported by Keith Flanagan in post #5.
The code in post #1 is OK, with the correct lat / lng values!
I tested it!

Just as a reminder:
B4X:
Dim BigBenLongitude As Double = 51.500770 ' longitude'
Dim BigBenLatitude As Double = -0.124622
      
Dim TescoLat As Double = 51.501137 '<<<< latitude
Dim TescoLong As Double = -0.125092
 
Upvote 0

netsistemas

Active Member
Licensed User
Longtime User
I use this code (i'm not sure that mi code shared are ok). i don't know this other funcion.


B4X:
Sub  Distancia(lat1 As Double, lon1 As Double, lat2 As Double, lon2 As Double)  As Double
  Dim  p As Double = 0.017453292519943295  ' // Math.PI / 180
  Dim a  As Double = 0.5 - Cos((lat2 - lat1) * p) / 2 +     Cos(lat1  * p) * Cos(lat2 * p) *     (1 - Cos((lon2 - lon1) * p)) / 2

  Return 12742 * Sin(Sqrt(a))' // 2 * R; R = 6371 km
End Sub
 
Upvote 0

Keith Flanagan

Member
Licensed User
Thanks All

The issue was with my variables rather then the DistanceTo method.

In the original code in post 1 I have Lat and Long the wrong way around for Big Ben :)

My bad!
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I use this code (i'm not sure that mi code shared are ok). i don't know this other funcion.
Your code is OK for small distances.
It will give less precise values than DistanceTo.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Upvote 0
Top