SQL search using Haversine in Doctrine

拥有回忆 提交于 2019-12-23 03:24:25

问题


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.


回答1:


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");


来源:https://stackoverflow.com/questions/1973878/sql-search-using-haversine-in-doctrine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!