Calculating Distances from Latitude and Longitude
In this scenario, you're looking to determine the points within a 15-mile radius of the user-entered coordinates. To achieve this, you can leverage mathematical formulas to calculate distances.
Consider the following PHP function:
function get_distance($latitude1, $longitude1, $latitude2, $longitude2, $unit = 'Mi') {
// Calculate the angle difference
$theta = $longitude1 - $longitude2;
// Calculate the distance
$distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2)))
(cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) *
cos(deg2rad($theta)));
// Convert to degrees
$distance = acos($distance);
$distance = rad2deg($distance);
// Convert to miles or kilometers
$distance = $distance * 60 * 1.1515;
// Round the distance
return (round($distance,2));
}
Alternatively, you can utilize a database query to accomplish this:
$query = "SELECT *,(((acos(sin((".$latitude."*pi()/180)) *
sin((`Latitude`*pi()/180)) cos((".$latitude."*pi()/180)) *
cos((`Latitude`*pi()/180)) * cos(((".$longitude."- `Longitude`)*
pi()/180))))*180/pi())*60*1.1515
) as distance
FROM `MyTable`
HAVING distance > ".$distance.";
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3