根据经纬度获取附近点位数据

发表于

接口链接:  /Api/Meet/Map/getPoint?latitude=30.478379&longitude=114.392454&lc=1

latitude:纬度

longitude:经度

lc:距离(km)

sql语句:

SELECT * FROM ( SELECT * , sqrt((114.392454 - gsm_longitude) * pi() * 12656 * cos((30.478379 + gsm_latitude) / 2 * pi() / 180) / 180 * ((114.392454 - gsm_longitude) * pi() * 12656 * cos((30.478379 + gsm_latitude) / 2 * pi() / 180) / 180) + (30.478379 - gsm_latitude) * pi() * 12656 / 180 * ((30.478379 - gsm_latitude) * pi() * 12656 / 180)) AS lc FROM _cabinet ) _cabinet WHERE lc < 10 ORDER BY lc

接口代码:

try {
            $longitude = $this->request()->getQueryParam('longitude');
            $latitude = $this->request()->getQueryParam('latitude');
            $lc = $this->request()->getQueryParam('lc');
            if (!$longitude || !$latitude || !$lc) {
                return $this->writeJson(401, [], '参数缺失');
            }
            $queryBuild = new QueryBuilder();
            // 支持参数绑定 第二个参数非必传
            $queryBuild->raw("SELECT * FROM ( SELECT * , sqrt(({$longitude} - gsm_longitude) * pi() * 12656 * cos(({$latitude} + gsm_latitude) / 2 * pi() / 180) / 180 * (({$longitude} - gsm_longitude) * pi() * 12656 * cos(({$latitude} + gsm_latitude) / 2 * pi() / 180) / 180) + ({$latitude} - gsm_latitude) * pi() * 12656 / 180 * (({$latitude} - gsm_latitude) * pi() * 12656 / 180)) AS lc FROM _cabinet ) _cabinet WHERE lc < ? ORDER BY lc", [abs($lc)]);

//            var_dump($queryBuild->getLastQuery());

            // $queryBuild->raw("select * from test where name = 'siam'");
            // 第二个参数 raw  指定true,表示执行原生sql
            // 第三个参数 connectionName 指定使用的连接名,默认 default
            $list = DbManager::getInstance()->query($queryBuild, true);
            $list = $list->getResult();
            $res = [];
            foreach ($list as $item) {
                $res[] = [
                    'id' => $item['id'],
                    'latitude' => $item['gsm_latitude'],
                    'longitude' => $item['gsm_longitude'],
                    'title' => $item['serial_num'],
                    'iconPath' => '/static/play.png',
                    'rotate' => 0,
                    'joinCluster' => true,
                    'width' => 20,
                    'height' => 20,
                    'anchor' => [
                        'x' => 0.5,
                        'y' => 1
                    ],
                ];
            }
            return $this->writeJson(200, $res, '点位列表');
        } catch (\Throwable $throwable) {
            return $this->writeJson(500, [], $throwable->getMessage());
        }

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注