cxfoot/modules/api/components/GetDistance.php
2023-10-27 14:25:12 +08:00

77 lines
2.1 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021-4-17
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\modules\api\components;
class GetDistance
{
/**
*计算两点地理坐标之间的距离
* @param /Decimal $longitude1 起点经度
* @param /Decimal $latitude1 起点纬度
* @param /Decimal $longitude2 终点经度
* @param /Decimal $latitude2 终点纬度
* @param Int $unit 单位 1:米 2:公里
* @param Int $decimal 精度 保留小数位数
* @return /Decimal
*/
public static function getDistance($longitude1, $latitude1, $longitude2, $latitude2, $unit = 2, $decimal = 2)
{
if (empty($longitude1) || empty($latitude1)) {
return "未开启定位";
}
$EARTH_RADIUS = 6370.996; // 地球半径系数
$PI = 3.1415926;
$radLat1 = $latitude1 * $PI / 180.0;
$radLat2 = $latitude2 * $PI / 180.0;
$radLng1 = $longitude1 * $PI / 180.0;
$radLng2 = $longitude2 * $PI / 180.0;
$a = $radLat1 - $radLat2;
$b = $radLng1 - $radLng2;
$distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
$distance = $distance * $EARTH_RADIUS * 1000;
if ($unit == 2) {
$distance = $distance / 1000;
}
return round($distance, $decimal) . 'km';
}
public static function mdate($time)
{
switch ($time) {
case $time == 0:
$text = '刚刚';
break;
//秒
case $time < 60:
$text = $time . '秒';
break;
//分钟
case $time < 60 * 60:
$text = floor($time / 60) . '分钟';
break;
//小时
case $time < 60 * 60 * 24:
$text = floor($time / (60 * 60)) . '小时';
break;
case $time > 60 * 60 * 24:
$text = floor($time / (60 * 60)) . '小时';
break;
}
return $text;
}
}