Geo: How to calculate Point B given Point A lat+long + Bearing and Distance

PD5DJ

Member
Licensed User
Longtime User
Hi all,

Im headbreaking over the following case.

I want to know a new lattitude and longitude location.
Parameters given are:

Longitude + Lattitude + Bearing + Distance

I found some code on the internet, but i cant seems to figure out why it is not working..
I get really strange outputs.. :sign0163:

My inputs are in HH.MMSS like 52.3919

Program is attached.
This is my code:
B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim Lattitude_input As EditText
   Dim Longitude_input As EditText
   Dim Distance_input As EditText
   Dim Bearing_input As EditText
   Dim Calc_button As Button
   Dim LatA, LonA As Double
   Dim LatB, LonB As Double
   Dim Distance, Bearing As Int
   Dim Radius As Int = 6372.795477598
   



   Dim Longitude_output As Label
   Dim lattitude_output As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("Layout1")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub Calc_button_down
   LatA = Lattitude_input.Text
   LonA = Longitude_input.Text
   Distance = Distance_input.Text
   Bearing = Bearing_input.Text
   
   Distance = (Distance /Radius)
   
   'LatB = ASin(Sin(LatA) * Cos(Distance/Radius) + Cos(LatA) * Sin(Distance/Radius) * Cos(Bearing))
   'LonB = LonA + ATan2(Sin(Bearing) * Sin(Distance / Radius) * Cos(LatA), Cos(Distance / Radius) - Sin(LatA) * Sin(LatB))
   
   LatB =ASin(Sin(LatA)*Cos(Distance/Radius) + Cos(LatA)*Sin(Distance/Radius)*Cos(Bearing))
   LonB = LonA + ATan2(Cos(Distance/Radius)-Sin(LatA)*Sin(LatB), Sin(Bearing)*Sin(Distance/Radius)*Cos(LatA)) 
   
   lattitude_output.Text = ""
   Longitude_output.Text = ""
   lattitude_output.Text = LatB
   Longitude_output.Text = LonB
   
      
End Sub
 

derez

Expert
Licensed User
Longtime User
I haven't checked the formula, but this is wrong:
My inputs are in HH.MMSS like 52.3919
If you use lat and long in the formula it must be decimal numbers of degrees, not the combination of degrees, minutes and seconds.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You are mixing up a few things:
- you need to convert the degrees to decimal degrees
- radians and degrees
- you calculate Distance = Distance / Radius and in the equations you use once again Distance / Radius
- you Dim Radius As Int = 6372.795477598 but 6372.795477598 is a Double !
- you Dim Distance As Int so 10 / 6372.795477598 = 0

This works:
B4X:
Dim Distance, Bearing As Double
Dim Radius As Double = 6372.795477598
'
'
'    Distance = (Distance /Radius)
LatB = ASinD(SinD(LatA) * Cos(Distance/Radius) + CosD(LatA) * Sin(Distance/Radius) * CosD(Bearing))
LonB = LonA + ATan2D(SinD(Bearing) * Sin(Distance / Radius) * CosD(LatA), Cos(Distance / Radius) - SinD(LatA) * SinD(LatB))
And this also:
B4X:
Dim Distance, Bearing As Double
Dim Radius As Double = 6372.795477598
'
'
Distance = Distance / Radius
LatB = ASinD(SinD(LatA) * Cos(Distance) + CosD(LatA) * Sin(Distance) * CosD(Bearing))
LonB = LonA + ATan2D(SinD(Bearing) * Sin(Distance) * CosD(LatA), Cos(Distance) - SinD(LatA) * SinD(LatB))
 
Upvote 0

PD5DJ

Member
Licensed User
Longtime User
Hi Klaus and David, thanks for the input and help. Really apreciated!

Too bad math is not my strongest point :( :sign0104:

But David pointed me out that im going wrong at point 1, entering the correct data..

My data is DD.MM.SS and needs to be converted to DD.MMMM

I found the following formula for that:

Decimal = degrees + (minutes/60) + (seconds / 3600)

So i need 3 variables, Degrees, Minutes and seconds..

How can i make these 3 variables from this Double? : 52.3919



Also when I have calculated my new position i have to convert it back to DD.MM.SS.

I found some php and VB.net code.. I understand the procedure, but I cant seem to translate it to this basic..

B4X:
Converting decimal degrees to degrees/minutes/seconds, and vice versa
It is easy to convert coordinates between DD and DMS. Here's the formula for converting from DD to DMS:
DD: dd.ff
DMS: dd mm ss

dd=dd
mm.gg=60*ff
ss=60*gg

Here, gg is the fractional part of the calculation. Negative latitude denotes a location in the Southern Hemisphere (S) and negative longitude is a location in the Western Hemisphere (W). For example, imagine that you have the coordinates (in DD format) of 61.44, 25.40. You'd convert them as follows:
lat dd=61
lat mm.gg=60*0.44=26.4
lat ss=60*0.4=24

And:
lon dd=25
lon mm.gg=60*0.40=24.0
lon ss=60*0.0=0
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
My OSMDroid library contains a GeoPoint object that has a method that will do all of this for you:

GeoPoint
Methods:
  • GetBearingTo (GeoPoint1 As GeoPoint) As Double
    Get the bearing (in degrees) from this GeoPoint to GeoPoint1.
  • GetDestinationPoint (Distance As Double, Bearing As Float) As GeoPointWrapper
    Get a new GeoPoint that is the specified Distance (in meters) and Bearing (in degrees) away from this GeoPoint.
  • GetDistanceTo (GeoPoint1 As GeoPoint) As Int
    Get the distance (in metres) from this GeoPoint to GeoPoint1.
  • Initialize (Latitude As Double, Longitude As Double)
    Initialize the GeoPoint using Latitude and Longitude.
  • Initialize2 (aLocation As Location)
    Initialize the GeoPoint using a Location object.
  • IsInitialized As Boolean
Properties:
  • Altitude As Int
    Get or Set the altitude (in meters) of the GeoPoint
  • Latitude As Double
    Get or Set the Latitude of the GeoPoint.
  • Longitude As Double
    Get or Set the Longitude of the GeoPoint.

That makes the B4A code much simpler:

B4X:
Dim GeoPointA, GeoPointB As GeoPoint

'   this assumes your values are already decimal degrees
GeoPointA.Initialize(Lattitude_input.Text, Longitude_input.Text)
GeoPointB=GeoPointA.GetDestinationPoint(Distance_input.Text, Bearing_input.Text)

Dim LatB As Double=GeoPointB.Latitude
Dim LonB As Double=GeoPointB.Longitude

Martin.
 
Upvote 0
Top