Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $DEG2RAD = 3.14159265359 / 180;
- $EARTHRAD = 6371000;
- function distbear( $a, $b ){
- // distance and bearing at point $a to go to point $b
- global $EARTHRAD;
- global $DEG2RAD;
- $_a = pow( sin( 0.5 * ( $b->lat - $a->lat ) ), 2 ) +
- cos( $b->lat ) * cos( $a->lat ) * pow( sin( 0.5 * ( $b->lng - $a->lng ) ), 2 );
- $_d = $EARTHRAD * 2 * atan2( sqrt( $_a ), sqrt( 1 - $_a ) );
- $_n = cos( $a->lat ) * sin( $b->lat ) - sin( $a->lat ) * cos( $b->lat ) * cos( $b->lng - $a->lng ); //north
- $_e = sin( $b->lng - $a->lng ) * cos( $b->lat ); //east
- $_b = atan2( $_e, $_n ); // FORMULA: bear = atan2( 90deg=east, 0deg=north ) //south=-north, west=-east
- return (object) array( 'distance'=>$_d, 'bearing'=>$_b );
- }
- $a = (object) array('lat' => $DEG2RAD * +0.0000, 'lng' => $DEG2RAD * +0.0000 );
- $b = (object) array('lat' => $DEG2RAD * +0.0000, 'lng' => $DEG2RAD * -1.0000 ); // west
- $b = (object) array('lat' => $DEG2RAD * +0.0000, 'lng' => $DEG2RAD * +1.0000 ); // east
- $b = (object) array('lat' => $DEG2RAD * +1.0000, 'lng' => $DEG2RAD * +0.0000 ); // north
- $b = (object) array('lat' => $DEG2RAD * -1.0000, 'lng' => $DEG2RAD * +0.0000 ); // south
- print_r( distbear( $a, $b ) );
Add Comment
Please, Sign In to add comment