本文介绍了在 Doctrine 中使用 Haversine 进行 SQL 搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过坐标进行搜索,即我想要一个像这样工作的函数:
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");
这篇关于在 Doctrine 中使用 Haversine 进行 SQL 搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!