在php / mysql中计算kml的标题

I have a table in mysql with 4 columns:

  • Latitude_1
  • Longitude_1
  • Latitude_2
  • Longitude_2

Now I want to calculate the heading for all rows to be used in a kml file. I found this function:

// Takes two sets of geographic coordinates in decimal degrees and produces bearing (azimuth) from the first set of coordinates to the second set.//

public static function bearing($lat1, $lon1, $lat2, $lon2) {
        $lat1 = deg2rad($lat1);
        $lon1 = deg2rad($lon1);
        $lat2 = deg2rad($lat2);
        $lon2 = deg2rad($lon2);
        $lonDelta = $lon2 - $lon1;
        $y = sin($lonDelta) * cos($lat2);
        $x = cos($lat1) * sin($lat2) - sin($lat1) * cos($lat2) * cos($lonDelta);
        $brng = atan2($y, $x);
        $brng = $brng * (180 / pi());

        if ( $brng < 0 ) { $brng += 360; }

        return $brng;
    }

Now I hope that someone shows me a query that echoes all headings (bearings) of the table based on the above mentioned function

  1. To combine an expression expr, take a look to the mysql math functions and find proper equivalents for php ones - https://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html
  2. Simplify your expr if possible
  3. Use your result expression expr in following query SELECT expr FROM your_table
  4. Profit