地理目的地公式

时间:2010-04-23 17:17:12

标签: php coordinates formula

我有这个PHP函数,但它似乎给出了错误的结果。有没有人知道它有什么问题(特别是公式)?

/**
 * 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 )
                )
        );
}

1 个答案:

答案 0 :(得分:1)

计算适用于弧度,因此您首先需要将基于度数的值转换为该值,然后再返回一个有意义的值。例如:

public static function findDestination(array $startingCoordinates, $bearing, $distance) {
        $startingCoordinates['lat'] = deg2rad((float)$startingCoordinates['lat']);
        $startingCoordinates['lon'] = deg2rad((float)$startingCoordinates['lon']);
        $bearing = deg2rad($bearing);
        $angularDistance = $distance / Maps_EARTH_RADIUS;
        $lat = asin(
            sin($startingCoordinates['lat']) * cos( $angularDistance) +
            cos($startingCoordinates['lat']) * sin( $angularDistance) * cos($bearing)
        );
        return array(
            'lat' => rad2deg($lat),
            'lon' => rad2deg($startingCoordinates['lon'] + atan2(
                    sin($bearing ) * sin($angularDistance ) * cos($startingCoordinates['lat']),
                    cos($angularDistance) - sin($startingCoordinates['lat'] ) * sin($lat)
            ))
        );
}

使用findDestination(array('lat' => 40.7142691, 'lon' => -74.0059729), 90, 1000)进行测试会得到40.714268492616,-73.994108059287的结果,我认为这更像是您正在寻找的。