Skip to content

Instantly share code, notes, and snippets.

@phillipwilhelm
Forked from zackkatz/get-radius.php
Created March 28, 2020 05:29
Show Gist options
  • Save phillipwilhelm/001a81f3cd7381ddf53483510950d680 to your computer and use it in GitHub Desktop.
Save phillipwilhelm/001a81f3cd7381ddf53483510950d680 to your computer and use it in GitHub Desktop.
Get a lat/long radius from passed latitude and longitude
<?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