美文网首页
计算距离

计算距离

作者: aleshaw | 来源:发表于2024-06-05 14:48 被阅读0次
public static function nearby($lat, $lng, $type)
    {
        $model = new ShopModel();
        $fields = 'id, type, name, logo, start_time, end_time, tags, address, desc, lng,lat,status,phone';
        if ($lat && $lng) {
            $model = $model->fieldRaw("$fields, ACOS(SIN(( $lat * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS(( $lat * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS(( $lng * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380 as distance")->order('distance');
        }
        if ($type) {
            $model = $model->where('type', $type);
        }
        $shop = $model->find();
        $shop->distance = $shop->distance > 1 ? round($shop->distance, 2).'km' : round($shop->distance * 1000) .'m';
        $shop->status_name = ShopModel::$status[$shop->status];
        return $shop;
    }

相关文章

网友评论

      本文标题:计算距离

      本文链接:https://www.haomeiwen.com/subject/hgmuqjtx.html