Last active
March 28, 2020 05:29
-
-
Save zackkatz/db0e01131a07bebc83e95b981073346b to your computer and use it in GitHub Desktop.
Get a lat/long radius from passed latitude and longitude
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 | |
| /** | |
| * Calculate the longitude and latitude or a radius around a location | |
| * | |
| * Returns an array with the following keys: `LatitudeMax`, `LongitudeMax`, `LatitudeMin`, `LongitudeMin`, to produce a radius search | |
| * | |
| * @param string|float $longitude The longitude to calculate from | |
| * @param string|float $latitude The latitude to calculate from | |
| * @param integer $miles Number of miles for the radius | |
| * | |
| * @return array Array with longitude-max | |
| */ | |
| function get_radius( $longitude, $latitude, $miles = 5 ) { | |
| /** | |
| * Latitude: degree is approximately 69.172 miles, and a minute of latitude is approximately 1.15 miles | |
| * Longitude: degree ~54.6 miles, minute ~0.91 miles | |
| * @see https://www.usgs.gov/faqs/how-much-distance-does-a-degree-minute-and-second-cover-your-maps | |
| */ | |
| $degrees = $miles / 69.172 / 100; | |
| $minutes = $miles / 1.15 / 100; | |
| $settings = array( | |
| 'LatitudeMax' => floatval( $latitude) + $degrees, | |
| 'LatitudeMin' => floatval( $latitude ) - $degrees, | |
| 'LongitudeMax' => floatval( $longitude ) + $minutes, | |
| 'LongitudeMin' => floatval( $longitude ) - $minutes, | |
| ); | |
| return $settings; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment