# Android QuestionHelp with some b4A Calculations

#### daniedb

##### Active Member
Longtime User
Hi Guys

I'm busy converting one of my windows Coordinates Programs for Racing Pigeons from a Windows program to B4A
I got stuck with some calc convertions.

B4X:
``````sub Globals
Dim Deg2Rad As Double  'EQUATE(.0174532925199) ! number of degrees In a radian
Dim a As Double  '  EQUATE(6378.14) ! the earth's radius in kilometers
Dim fl As Double  '  EQUATE(.003352813) ! the earth's flattening
Dim lat1 As Double  '  REAL ! latitude of departure
Dim lat2 As Double
Dim North1  As Long  '  Short(0) ! latitude of departure Northern Hemisphere
Dim South1  As Long  '  Short(1) ! latitude of departure Southern Hemisphere
Dim West1  As Long  '  Short(0) ! longitude of departure Western Hemisphere
Dim East1  As Long  '  Short(1) ! longitude of departure Eastern Hemisphere
Dim lon1 As Double  '  REAL ! longitude ofdeparture
Dim North2 As Long  '  Short(0) ! latitude of destination Northern Hemisphere
Dim South2 As Long  '  Short(1) ! latitude of destination Southern Hemisphere
Dim West2  As Long  '  Short(0) ! longitude of destination Western Hemisphere
Dim East2  As Long  '  Short(1) ! longitude of destination Eastern Hemisphere
Dim lon2  As Double  '  REAL ! longitude of destination

Dim F As Double  '  REAL ! (lat1 + lat2)/2
Dim G As Double  '  REAL ! (lat1 - lat2)/2
Dim lambda As Double  '  REAL ! (lon1 - lon2)/2
Dim S As Double  '  REAL
Dim C As Double  '  REAL
Dim tan_omega  As Double  ' REAL
Dim omega As Double  '  REAL
Dim R  As Double  '  REAL
Dim D As Double  '  REAL ! approxomite Distance In kilometers
Dim H1  As Double  '  REAL
Dim H2  As Double  '  REAL
Dim km  As Double  '  decimal(12,3)!REAL(0) ! the Distance In kilometers
Dim mi  As Double  '  REAL(0) ! the Distance In miles
Dim Meter As Double  '  Decimal(12,2)
Dim coord As Double

end sub``````

'* - lines working
'X - lines with errors

B4X:
``````Sub Distance
F = (lat1 + lat2)/2    '*
G = (lat1 - lat2)/2    '*
lambda = (lon1 - lon2)/2

S = Sin(G)^2 * Cos(lambda)^2 + Cos(F)^2 * Sin(lambda)^2   'X
C = Cos(G)^2 * Cos(lambda)^2 + Sin(F)^2 * Sin(lambda)^2   'X

tan_omega = Sqrt(S/C) '*

omega = ATan(tan_omega) '! In radians '*

D = 2*omega*a ' approximate Distance In kilometers   '*
R= Sqrt(S*C)/omega    '*
H1 = (3*R - 1)/(2*C)  '*
H2 = (3*R + 1)/(2*S)   '*

km = D *(1 + fl*H1*Sin(F)^2 * Cos(G)^2 - fl*H2*Cos(F)^2 * Sin(G)^2)  'X
mi = km/1.609344
Meter = km * 1000
End Sub``````

If interested, here is my original WIndows App
This program is pretty accurate, for distance between 2 Coordinates

Thanks, Appreciate
Danie

Last edited:

#### derez

##### Expert
Longtime User
The coordinates should be of type double, not long
Sin(G)^2 -> power(sin(G) , 2)

I think that type "location" with these methods will give you what you need easily :

Initialize2 (Latitude As String, Longitude As String)
Initializes the location object with the given Latitude and Longitude.
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")

DistanceTo (TargetLocation As android.location.Location) As Float
Returns the distance to the given location, measured in meters.

Last edited:

#### daniedb

##### Active Member
Longtime User
Thanks Derez
I'll have a look at Location.

Thanks for the conversion help, appreciate
Danie

#### derez

##### Expert
Longtime User
You can also save on translating deg to rad by using trig functions with D : SinD(deg) instead of sin(rad) etc.

#### daniedb

##### Active Member
Longtime User
Derez

Had a look at location. Will defnitily use in the future
Do I understand this correctly.
Initialize2, will take the lat/lon entered
Distanceto, will give the distance from current gps location to Coordinates in Initialize2?

Correct?

I want to enter 2 different coordinates (No GPS required) and workout the distance!
Dont see any function for this in Location?

Thanks
Danie

#### derez

##### Expert
Longtime User
Initialize2 tells Location "where it is". It is not connected to gps unless you put the gps coordinates there by code.
DistanceTo measures the distance from "where it is" to the target. So, each target you put in DistanceTo will measure the distance to that target, endless possible number of points...

#### daniedb

##### Active Member
Longtime User
Thanks Derez.
Pitty I haven't found this earlier. Would save me hours.

Got is going, Appreciate your help

It doesn't seem that accurate, right? or my "real" GPS is not accurate?
I'm out by about 100 meters, but thats close enough for what I want

Cheers
Danie

#### derez

##### Expert
Longtime User
GPS accuracy should be much better at least 10-30 m, but that is when you have good reception of sattelites.
Make sure you are not rounding the data by using int or long instead of double.