Skip to content

Instantly share code, notes, and snippets.

@andxbes
Last active November 10, 2025 12:12
Show Gist options
  • Select an option

  • Save andxbes/fdc2ba73db907f095a31be62a72b9d5c to your computer and use it in GitHub Desktop.

Select an option

Save andxbes/fdc2ba73db907f095a31be62a72b9d5c to your computer and use it in GitHub Desktop.
calculate pointer distance
<?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