4

Let's say I have the latitude and longitude coordinate [30, -87]. From that point, I would like to be able to find where my location would be after traveling let's say 10km at 45 degrees of bearing.

My first attempt at trying to solve this didn't go so well as you can see below. This obviously doesn't account for the units which is really the hardest part to comprehend for me at least.

var bearing = 45;
var distance = 10;

var position = {
 "latitude": 30,
 "longitude": -87
};

var newLat = Math.cos(bearing) * distance + position.latitude;
var newLon = Math.sin(bearing) * distance + position.longitude;

I'm assuming the radius of the earth will come into play at one point but I really didn't know where to start so any help would be greatly appreciated.

lmg1114
  • 193
  • 1
  • 6
  • It's an integration problem. You can approximate the earth to a sphere and use the spherical coordinates. cos(bearing) gives change along azimuthal angle. sin(bearing) gives change along polar angle. I feel someone at math stack exchange could give a more detailed explanation than one you'd get on stackoverflow – Abhay Aravinda Aug 28 '21 at 05:44
  • or gis. There are a lot of links to various levels of complexity of explanation, and they use different programming languages in discussion. https://gis.stackexchange.com/questions/5821/calculating-latitude-longitude-x-miles-from-point – Chris Strickland Aug 28 '21 at 05:58

1 Answers1

4

I have used python code here: Get lat/long given current point, distance and bearing, and converted in into JS code to get the answer.

Here is the code:

const bearing = 45;
const bearing_rad = (bearing*Math.PI)/180;
const distance = 10;

const EARTH_RADIUS = 6378.137;

const initial_position = {
 "latitude": 30,
 "longitude": -87
};

const init_lat = (initial_position.latitude*Math.PI)/180;
const init_lon = (initial_position.longitude*Math.PI)/180;

const final_lat = (180/Math.PI)*(Math.asin( Math.sin(init_lat)*Math.cos(distance/EARTH_RADIUS) + Math.cos(init_lat)*Math.sin(distance/EARTH_RADIUS)*Math.cos(bearing_rad)));

const final_lon = (180/Math.PI)*(init_lon + Math.atan2(Math.sin(bearing_rad)*Math.sin(distance/EARTH_RADIUS)*Math.cos(init_lat), Math.cos(distance/EARTH_RADIUS)-Math.sin(init_lat)*Math.sin(final_lat)));

console.log(final_lat, final_lon); // 30.06350049619102 -86.96303358059173
Rishabh Gupta
  • 734
  • 6
  • 10
  • 1
    Probably negligible, but if you need absolute precision, you can set Earth's radius to 3 decimal places, EARTH_RADIUS = 6378.137 - from Google Maps' geometry library docs "The default radius is Earth's radius of 6378137 meters" - https://developers.google.com/maps/documentation/javascript/reference/geometry – Esteban Ortega May 01 '23 at 22:46