Search code examples
phpmathlatitude-longitudecoordinate-systems

Computing coordinates from longitude, latitude, speed and heading


I've been trying to solve this problem for the last 24 hours now...

Here are the hypothesis :


Current longitude (in decimal format): 53.3205556

Current latitude (in decimal format): -1.7297223

Distance traveled in kilometers: 1400km

Heading in degrees: 45°


I would like to compute the new coordinates in decimals after 1400 kilometers. Here is my code (in PHP).

function calc_position($latitude, $longitude, $distance, $heading){

    $r = 6378;
    // Earth circonference
    // Convert heading in degres to radians
    $bearing = deg2rad($heading); 

    // Compute new latitude and longitude
    $latitude2 =  asin( (sin($latitude) * cos($distance/$r)) + (cos($latitude) * sin($distance/$r) * cos($bearing)) );
    $longitude2 = $longitude + atan2( sin($bearing)*sin($distance/$r)*cos($latitude), cos($distance/$r)-(sin($latitude)*sin($latitude2)) );

    return (object) array(
        'latitude' => $latitude2,
        'longitude' => $longitude2
    );
}

Unfortunately the results for new longitude and latitude are wrong (not consistent displayed on a map). I have thoroughly followed a tutorial HERE to try this formula but i can't come across the solution despite this website.


Solution

  • Unless you did it outside this function, you also need to convert the initial lat/long to radians, and the result of your formulae back to degrees.