Files
anmo/server/app/common/logic/CityLogic.php
2025-08-19 14:16:51 +08:00

40 lines
1.2 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\common\logic;
use app\common\model\city\City;
/**
* 城市逻辑类
* Class CityLogic
* @package app\common\logic
*/
class CityLogic extends BaseLogic
{
/**
* @notes 获取经纬度附近的城市
* @param $longitude
* @param $latitude
* @return array
* @author cjhao
* @date 2024/9/3 23:41
*/
public static function getNearbyCity($longitude,$latitude)
{
//用st_distance_sphere函数计算两点记录单位米这里换成千米
$field = 'id,city_id,name,0 as distance,gcj02_lng as longitude,gcj02_lat as latitude';
if($longitude && $latitude){
//用st_distance_sphere函数计算两点记录单位米这里换成千米
$field = 'id,city_id,name,round(st_distance_sphere(point('.$longitude.','.$latitude.'),
point(gcj02_lng, gcj02_lat))/1000,2) as distance,'.$longitude.' as longitude,'.$latitude.' as latitude';
}
$cityLists = City::field($field)
->append(['distance_desc'])
->order('distance asc')
->hidden(['distance'])
->select()
->toArray();
return $cityLists;
}
}