PHP在一定半径内选择随机lon/lat [英] PHP Select a random lon/lat within a certain radius

查看:60
本文介绍了PHP在一定半径内选择随机lon/lat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个lon/lat: 33.33333,22.22222

Let's say I have this lon/lat: 33.33333,22.22222

如何在X英里/公里半径内随机选择另一个lon/lat?

How can I randomly select another lon/lat within an X miles/km radius?

谢谢

推荐答案

您可以使用这篇文章来帮助您:

You could use this post to help guide you along:

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

因此,在您的示例中,您只需选择1到10英里之间的随机数,其中10是您的在一定半径内".

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;

更新:

正如Tomalak在下面的评论中所述,这是在地球是球形而不是不平坦的大地水准面的假设下进行的.因此,您将获得近似值,而不是潜在的(接近)确切结果.

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.

这篇关于PHP在一定半径内选择随机lon/lat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆