SQL搜索使用Haversine在Doctrine [英] SQL search using Haversine in Doctrine
本文介绍了SQL搜索使用Haversine在Doctrine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
function getLocationsInCircle ($ lat,$ long,$ minDist,$ maxDist){
//返回所有至少$ minDist
//公里的地方,不超过$ maxDist公里
我有一个位置表,用于存储所有位置ID及其纬度和经度。
haversine公式对我想要做的很好。
6371 * ACOS(SIN(RADIANS($ lat))* SIN(RADIANS(纬度))+ COS(RADIANS($ lat))* COS(RADIANS(纬度))* COS(RADIANS(经度) - RADIANS )))
我只是看不到如何在Doctrine中运行该查询。
解决方案尝试这样:
Doctrine_query: :create() - > select('id')
- > addSelect((6371 * ACOS(SIN(RADIANS($ latitude))* SIN(RADIANS(l.latitude))+ ($纬度))* COS(RADIANS(l.latitude))* COS(RADIANS(l.longitude) - RADIANS($ longitude))))作为距离)
- > from('Location l' )
- > having(Distance> $ minDist AND Distance< $ maxDist);
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
}
I have a "location" table that stores all location Ids and their latitude and longitude.
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 )))
I just don't see how to run that query in Doctrine.
解决方案 Try this :
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");
这篇关于SQL搜索使用Haversine在Doctrine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文