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

2018-08-24 11:00 By "Powerless" 2975 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);
}


评 论

View in WeChat

Others Discussion

  • PHP没你想的那么差
    Posted on 2021-12-17 15:40
  • Mysql联合索引的最左前缀匹配原则
    Posted on 2018-08-25 15:00
  • 2016年云计算热词
    Posted on 2019-06-12 17:53
  • 分布式架构之「 数据分布」
    Posted on 2019-11-14 10:00
  • 巧用CAS解决数据一致性问题
    Posted on 2019-03-07 11:55
  • PHP扩展ImageMagick安装
    Posted on 2022-11-11 11:16
  • 通过信鸽来解释HTTPS
    Posted on 2018-10-22 13:56
  • HTTP和HTTPS的区别
    Posted on 2020-08-10 23:00