views:

38

answers:

0

I have this PHP function, but it seems to give wrong results. Does anybody have an idea what's wrong with it (esp the formula)?

/**
 * Finds a destination given a starting location, bearing and distance.
 * 
 * @param array $startingCoordinates The starting coordinates, as non-directional floats in an array with lat and lon keys.
 * @param float $bearing The initial bearing in degrees.
 * @param float $distance The distance to travel in km.
 * 
 * @return array The desitination coordinates, as non-directional floats in an array with lat and lon keys.
 */
public static function findDestination( array $startingCoordinates, $bearing, $distance ) {
        $startingCoordinates['lat'] = (float)$startingCoordinates['lat'];
        $startingCoordinates['lon'] = (float)$startingCoordinates['lon'];
        $angularDistance = $distance / Maps_EARTH_RADIUS;
        $lat = asin(
                        sin( $startingCoordinates['lat'] ) * cos( $angularDistance ) +
                        cos( $startingCoordinates['lat'] ) * sin( $angularDistance ) * cos( $bearing )
        );
        return array(
                'lat' => $lat,
                'lon' => $startingCoordinates['lon'] + atan2(
                        sin( $bearing ) * sin( $angularDistance ) * cos( $startingCoordinates['lat'] ),
                        cos( $angularDistance ) - sin( $startingCoordinates['lat'] ) * sin( $lat )
                )
        );
}