如何在CakePHP中实现距离查询? [英] How to implement distance queries in CakePHP?

查看:71
本文介绍了如何在CakePHP中实现距离查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的查询(来自Google Maps):

I have a query (from Google Maps) like so:

SELECT ( 3959 * acos( cos( radians(MY_LAT) ) * cos( radians( LATITUDE ) ) * 
cos( radians( LONGITUDE ) - radians(MY_LONG) ) + sin( radians(MY_LAT) ) * sin( radians( 
LATITUDE ) ) ) ) AS distance
FROM zips
ORDER BY distance

我宁愿这是一个习惯

MY_LAT和MY_LONG是传入的参数。
LATITUDE和LONGITUDE是zips表中的列。

MY_LAT and MY_LONG are params passed in. LATITUDE and LONGITUDE are columns in zips table.

推荐答案

您可以使用地理编码器行为( https://github.com/dereuromark/tools/blob/2.0/Model/Behavior/GeocoderBehavior.php#L208 )和

You can use a behavior like the geocoder behavior ( https://github.com/dereuromark/tools/blob/2.0/Model/Behavior/GeocoderBehavior.php#L208 ) and attach it to your model with

$this->Model->Behaviors->attach('Tools.Geocoder');

并致电

$this->Model->setDistanceAsVirtualField($lat, $lng, $fieldName);

这样,将附加一个虚拟字段,并将像其他任何普通字段一样检索并包含您的距离值。您还可以按该字段进行排序/过滤。

this way a virtual field is attached and will retrieve and contain your distance value just like any other normal field. you can also sort/filter by that field then.

这篇关于如何在CakePHP中实现距离查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆