如何在CDBCriteria格式>>
中使用此查询SET @orig_lat=122.4058;
SET @orig_lon=37.7907;
SET @dist=10;
SELECT *
, 3956
* 2
* ASIN(SQRT(POWER(SIN((@orig_lat - abs(dest.latitude)) * pi()/180 / 2),2) + COS(@orig_lat * pi()/180) * COS(ABS (dest.latitude) * pi()/180) * POWER(SIN((dest.longitude - @orig_lon) * pi()/180 / 2), 2))) distance
FROM business
HAVING distance < @dist
ORDER
BY distance
LIMIT 10;
有人问过here。 但我没有得到答案。
答案 0 :(得分:0)
考虑将“Haversine”公式放入MySQL函数中以开始:
CREATE FUNCTION `HAVERSINE`(origin_lat FLOAT, origin_long FLOAT, dest_lat FLOAT, dest_long FLOAT)
RETURNS float
DETERMINISTIC
BEGIN
DECLARE R FLOAT;
SET R = 3956 * 2 * ASIN(SQRT(POWER(SIN((origin_lat - dest_lat) * pi()/180 / 2), 2) + COS(origin_lat * pi()/180) * COS(dest_lat * pi()/180) * POWER(SIN((origin_long-dest_long) * pi()/180 / 2), 2))) * 1.609344;
RETURN R;
END;
这样你就可以在查询中调用它。
然后,您可以通过创建存储过程来进一步抽象以获取最近的业务。类似的东西:
CREATE PROCEDURE `findClosestBusinessesToLatLng`(lat FLOAT, lng FLOAT)
BEGIN
SELECT *, HAVERSINE(lat, lng, businesses.lat, businesses.lng) AS dist
FROM businesses
ORDER BY dist
END
然后你的查询就变成了
Call(findClosestBusinessesToLatLng(122.4058,37.7907));