如何使用PHP检查某个坐标是否落入另一个坐标半径 [英] How to check if a certain coordinates fall to another coordinates radius using PHP only

查看:88
本文介绍了如何使用PHP检查某个坐标是否落入另一个坐标半径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过这么多的函数,但它恰好只适用于MySQL或Postgresql。我想要PHP的等价逻辑。我正在做一些比较,
就像我有这些数据是在创建时产生的。

 纬度:56.130366 
Long:-106.34677099999

稍后,我想检查这个坐标是否会落入

>纬度:57.223366
长度:-106.34675644699
半径:100000(米)


解决方案

感谢您的帮助。下面是一个示例函数,它使用两组经度和纬度坐标并返回两者之间的距离。

  function getDistance ($ latitude1,$ longitude1,$ latitude2,$ longitude2){
$ earth_radius = 6371;

$ dLat = deg2rad($ latitude2 - $ latitude1);
$ dLon = deg2rad($ longitude2 - $ longitude1);

$ a = sin($ dLat / 2)* sin($ dLat / 2)+ cos(deg2rad($ latitude1))* cos(deg2rad($ latitude2))* sin($ dLon / 2)* sin($ dLon / 2);
$ c = 2 * asin(sqrt($ a));
$ d = $ earth_radius * $ c;

返回$ d;
}

$ distance = getDistance(56.130366,-106.34677099999,57.223366,-106.34675644699);
if($ distance <100){
echo100公里范围内;
} else {
echo100公里半径以外;
}


I have seen so many functions but it happens to work only for MySQL or Postgresql. I want the equivalent logic for PHP. I'm doing some comparisons, like I have this data that were being produced when created.

Lat: 56.130366
Long: -106.34677099999

Later on, I want to check if this coordinates will fall within a radius of another coordinates then returns true, otherwise false.

Lat: 57.223366
Long: -106.34675644699
radius: 100000 ( meters )

Thanks in advance!

解决方案

Thanks for the help. Below is an example function that takes two sets of longitude and latitude co-ordinates and returns the distance between the two.

function getDistance( $latitude1, $longitude1, $latitude2, $longitude2 ) {  
    $earth_radius = 6371;

    $dLat = deg2rad( $latitude2 - $latitude1 );  
    $dLon = deg2rad( $longitude2 - $longitude1 );  

    $a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * sin($dLon/2) * sin($dLon/2);  
    $c = 2 * asin(sqrt($a));  
    $d = $earth_radius * $c;  

    return $d;  
}

$distance = getDistance( 56.130366, -106.34677099999, 57.223366, -106.34675644699 );
if( $distance < 100 ) {
    echo "Within 100 kilometer radius";
} else {
    echo "Outside 100 kilometer radius";
}

这篇关于如何使用PHP检查某个坐标是否落入另一个坐标半径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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