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
B4A includes all the features needed to quickly develop any type of Android app. B4A is used by tens of thousands of developers from all over the world, including companies such as NASA, HP, IBM and others.
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
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
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")
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.
Just to clarify.
The problem was not the variable !
But the inversion between latitude and longitude values !!!
As already reported by Keith Flanaganin 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
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