在 Doctrine 中使用 Haversine 进行 SQL 搜索

2022-01-16 00:00:00 coordinates php doctrine

我想通过坐标进行搜索,即我想要一个像这样工作的函数:

I want to do a search by coordinates, i.e. I want to have a function that works like this:

function getLocationsInCircle($lat, $long, $minDist, $maxDist){
    //return all the places that are at least $minDist 
    //kilometers away and no more than $maxDist kilometers away
}

我有一个位置"表,用于存储所有位置 ID 及其纬度和经度.

I have a "location" table that stores all location Ids and their latitude and longitude.

harsine 公式足以满足我想要做的事情

The haversine formula is good enough for what I want to do

6371 * ACOS(SIN(RADIANS( $lat )) * SIN(RADIANS( latitude )) + COS(RADIANS( $lat )) * COS(RADIANS( latitude )) * COS(RADIANS( longitude ) - RADIANS( $long )))

我只是不知道如何在 Doctrine 中运行该查询.

I just don't see how to run that query in Doctrine.

推荐答案

试试这个:

Doctrine_query::create()->select('id')
    ->addSelect("(6371 * ACOS(SIN(RADIANS($latitude)) * SIN(RADIANS(l.latitude)) + COS(RADIANS($latitude)) * COS(RADIANS(l.latitude)) * COS(RADIANS(l.longitude) - RADIANS($longitude)))) as Distance")
    ->from('Location l')
    ->having("Distance > $minDist AND Distance < $maxDist");

相关文章