How to check if a certain coordinates fall to another coordinates radius using PHP only
Thanks for the help. Below is an example function that takes two sets of longitude and latitude co-ordinates and returns the distance between the two.
function getDistance($latitude1, $longitude1, $latitude2, $longitude2) { $earth_radius = 6371; $dLat = deg2rad($latitude2 - $latitude1); $dLon = deg2rad($longitude2 - $longitude1); $a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * sin($dLon/2) * sin($dLon/2); $c = 2 * asin(sqrt($a)); $d = $earth_radius * $c; return $d; }$distance = getDistance(56.130366, -106.34677099999, 57.223366, -106.34675644699);if ($distance < 100) { echo "Within 100 kilometer radius";} else { echo "Outside 100 kilometer radius";}
You should use Haversine formula to compute distance between two points. You have a PHP version here.
Then just check if distance < 100000
.
This should help,
$lat_origin = 56.130366;$long_origin = -106.34677099999;$lat_dest = 57.223366;$long_dest = -106.34675644699;$radius = 3958; # Earth's radius (miles, convert to meters)$deg_per_rad = 57.29578; # Number of degrees/radian (for conversion)$distance = ($radius * pi() * sqrt( ($lat_origin - $lat_dest) * ($lat_origin - $lat_dest) + cos($lat_origin / $deg_per_rad) # Convert these to * cos($lat_dest / $deg_per_rad) # radians for cos() * ($long_origin - $long_dest) * ($long_origin - $long_dest) ) / 180);