Thank you very much, Paul!
I tried to encapsulate the complete function and variables a bit more, and extended the examples.
Hopefully all is done the right way and the results are true
(e.g I'm not sure, why the latitude changes a bit in the 3rd example..., or if the +/- of the resulting lat/long are always handled correctly...)
My own test inside my project will take a bit...
[Update:
9th Feb. 2019]
Code: Select all
Define.d Distance, Bearing
Structure geopoint
Latitude.d
Longitude.d
EndStructure
Define pos.geopoint
#MostNorthern = 0
#MostSouthern = 180
#MostEastern = 90
#MostWestern = 270
Procedure DistanceFromPoint(*pos.geopoint, Distance, Bearing)
; This function calculates the new geographic point in the 'Distance' (of xxx km) from the
; given current geographic point '@pos'.
; 'Bearing' determines the direction, where the new position should be calculated:
; #MostNorthern and #MostSouthern will give a new latitude value
; #MostEastern and #MostWestern will give a new longitude value
Protected Radius.d = 6372.795477598 ; a fixed value
Protected OldLatitude.d = *pos\Latitude, OldLongitude.d = *pos\Longitude
Protected NewLatitude.d, NewLongitude.d
; Now we use the given values (old Lat/Long position, Distance and Bearing/Direction) for calculating the new position:
NewLatitude = Degree(ASin(Sin(Radian(OldLatitude))*Cos(Distance/Radius)+Cos(Radian(OldLatitude))*Sin(Distance/Radius)*Cos(Radian(Bearing)) ))
NewLongitude = OldLongitude + Degree(ATan2(Cos(Distance/Radius)-Sin(Radian(OldLatitude))*Sin(Radian(NewLatitude)),Sin(Radian(Bearing))*Sin(Distance/Radius)*Cos(Radian(OldLatitude))))
; Return the new position by saving it in the structured variable (which can be accessed in the main program):
*pos\Latitude = NewLatitude
*pos\Longitude = NewLongitude
EndProcedure
; 1st example - going to the North:
pos\Latitude = 49
pos\Longitude = -94
Distance = 200
Debug "Old latitude/longitude: " + pos\Latitude + " / " + pos\Longitude
DistanceFromPoint(@pos, Distance, #MostNorthern) ;<-- Most northern point 200 km from lat/lon
Debug "..new latitude/longitude " + Distance + " km into North: " + pos\Latitude + " / " + pos\Longitude
Debug "----------------"
; 2nd example - going to the South:
pos\Latitude = 49
pos\Longitude = -94
Distance = 20
Debug "Old latitude/longitude: " + pos\Latitude + " / " + pos\Longitude
DistanceFromPoint(@pos, Distance, #MostSouthern) ;<-- Most southern point 20 km from lat/lon
Debug "..new latitude/longitude " + Distance + " km into South: " + pos\Latitude + " / " + pos\Longitude
Debug "----------------"
; 3rd extended example - going from Northpole (latitude = +90) to Southpole (latitude = -90) and calculate the new longitude for going 200 km to the East:
Debug "Here a more extended example: starting point moves from North to South (latitude decreases 10 degress everytime), but with constant longitude (20):"
For a = 85 To -85 Step -10
pos\Latitude = a
pos\Longitude = 20
Distance = 200
Debug "Old latitude/longitude: " + pos\Latitude + " / " + pos\Longitude
DistanceFromPoint(@pos, Distance, #MostEastern) ;<-- Most eastern point 200 km from lat/lon
Debug "..new latitude/longitude " + Distance + " km into East: " + pos\Latitude + " / " + pos\Longitude
Next
Debug "----------------"
; 4th example - going to the West:
pos\Latitude = 49
pos\Longitude = -94
Distance = 20
Debug "Old latitude/longitude: " + pos\Latitude + " / " + pos\Longitude
DistanceFromPoint(@pos, Distance, #MostWestern) ;<-- Most western point 20 km from lat/lon
Debug "..new latitude/longitude " + Distance + " km into West: " + pos\Latitude + " / " + pos\Longitude