PHP实现-计算两个坐标的距离

2018-08-24 11:00 By "Powerless" 3023 0 2

/**
 * 计算两个坐标之间的距离(米)
 * @param  [array]   $from  [起点坐标(经纬度)]
 * @param  [array]   $to    [终点坐标(经纬度)]
 * @return int
 */
private function getDistance($from,$to)
{
    sort($from);
    sort($to);
    $radius=6371393;
    $lat1=($from[0]* pi()) / 180;
    $lng1=($from[1]* pi()) / 180;
    $lat2=($to[0]* pi()) / 180;
    $lng2=($to[1]* pi()) / 180;
    $stepOne= pow(sin(($lat2-$lat1)/2),2) + cos($lat1) * cos($lat2) * pow(sin(($lng2-$lng1)/2),2);
    $stepTwo=2* asin(min(1, sqrt($stepOne)));
    return round($radius*$stepTwo);
}


评 论

Others Discussion

  • 2018年云计算热词
    Posted on 2019-06-12 18:19
  • PHP 基金会来啦!
    Posted on 2022-10-08 17:40
  • 快速了解Kafka
    Posted on 2021-03-25 14:20
  • Redis各种数据类型的使用场景举例分析【二】
    Posted on 2018-11-22 10:30
  • TCP协议的特性
    Posted on 2019-04-26 16:46
  • 让你的PHP7更快(GCC PGO)
    Posted on 2018-03-07 14:09
  • Composer 异常 [ErrorException]
    Posted on 2019-11-25 17:55
  • BASE原则
    Posted on 2020-12-17 16:42