Quantcast
Viewing latest article 4
Browse Latest Browse All 5

Answer by Mike Lewis for PHP Select a random lon/lat within a certain radius

You could use this post to help guide you along:

http://blog.fedecarg.com/2009/02/08/geo-proximity-search-the-haversine-equation/

So with your example, you would just pick a random number between 1 and 10 miles, where 10 is your "within a certain radius".

$longitude = (float) 33.33333;$latitude = (float) 22.22222;$radius = rand(1,10); // in miles$lng_min = $longitude - $radius / abs(cos(deg2rad($latitude)) * 69);$lng_max = $longitude + $radius / abs(cos(deg2rad($latitude)) * 69);$lat_min = $latitude - ($radius / 69);$lat_max = $latitude + ($radius / 69);echo 'lng (min/max): ' . $lng_min . '/' . $lng_max . PHP_EOL;echo 'lat (min/max): ' . $lat_min . '/' . $lat_max;

Update:

As Tomalak stated in the comments below, this is working under the assumption that the earth is a sphere rather than a uneven geoid. Because of this, you will get approximations rather than potentially (near)exact results.


Viewing latest article 4
Browse Latest Browse All 5

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>