40 lines
1.2 KiB
PHP
Executable File
40 lines
1.2 KiB
PHP
Executable File
<?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;
|
||
|
||
}
|
||
} |