Last active
November 10, 2025 12:12
-
-
Save andxbes/fdc2ba73db907f095a31be62a72b9d5c to your computer and use it in GitHub Desktop.
calculate pointer distance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function _calculateDistance( $lat1, $lon1, $lat2, $lon2 ) { | |
| // Haversine formula implementation here... | |
| $result = null; | |
| if ( | |
| $lat1 !== '' | |
| && $lon1 !== '' | |
| && $lat2 !== '' | |
| && $lon2 !== '' | |
| ) { | |
| $earth_radius = 6371; // km | |
| $dLat = deg2rad( $lat2 - $lat1 ); | |
| $dLon = deg2rad( $lon2 - $lon1 ); | |
| $a = sin( $dLat / 2 ) * sin( $dLat / 2 ) + cos( deg2rad( $lat1 ) ) * cos( deg2rad( $lat2 ) ) * sin( $dLon / 2 ) * sin( $dLon / 2 ); | |
| $c = 2 * atan2( sqrt( $a ), sqrt( 1 - $a ) ); | |
| $result = $earth_radius * $c; | |
| $result = round( $result, 1 ); | |
| // error_log( " $lat1, $lon1, $lat2, $lon2 = $result " ); | |
| } | |
| return $result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment