Distance At Bearing

Distance At Bearing

Method to calculate the a new point given the starting point, distance from initial point and the bearing

/**
 * [markerAtBearingAndDistance Calculates the lat lng of new point using bearing and distance]
 * @param  {Object} startPoint            [Lat,Lng of starting point]
 * @param  {Number} initialBearingRadians [Bearing in Radians]
 * @param  {Number} distanceKilometres    [Distance in Km]
 * @return {Object}                       [Lat,Lng Object of new point]
 */
function markerAtBearingAndDistance(startPoint, initialBearingRadians, distanceKilometres){
  // Gives a more accurate result see distance calculations within the UI
  // Earth is flat, I mean svery slightly ellipsoidal spherical model below is a little off 
  distanceKilometres = distanceKilometres * 0.9988825890739457;
  var radiusEarthKilometres = 6371.01;
  var distRatio = distanceKilometres / radiusEarthKilometres;
  var distRatioSine = Math.sin(distRatio);
  var distRatioCosine = Math.cos(distRatio);
  
  var startLatRad = degreesToRadians(startPoint.lat);
  var startLonRad = degreesToRadians(startPoint.lng);
  
  var startLatCos = Math.cos(startLatRad);
  var startLatSin = Math.sin(startLatRad);
  
  var endLatRads = Math.asin((startLatSin * distRatioCosine) + (startLatCos * distRatioSine * Math.cos(initialBearingRadians)));
  
  var endLonRads = startLonRad + Math.atan2(
    Math.sin(initialBearingRadians) * distRatioSine * startLatCos,
    distRatioCosine - startLatSin * Math.sin(endLatRads));
    
    return{
      lat: radiansToDegrees(endLatRads),
      lng: radiansToDegrees(endLonRads)
    };
  }


Share Tweet Send
0 Comments
Loading...