地理搜索mysql

时间:2012-05-22 17:48:48

标签: mysql search geo

我一直关注这个example进行MySQL的Geo搜索(非常棒的教程!)。

这就是我提出的:

CREATE DEFINER=`root`@`localhost` PROCEDURE `geo_distance`(in _location_id bigint(20), in dist int)
BEGIN
declare mylon double; 
declare mylat double;
declare lon1 float; 
declare lon2 float;
declare lat1 float; 
declare lat2 float;

-- get the original lon and lat for the userid 
select longitude, latitude into mylon, mylat from locations where locations.location_id = _location_id;
-- calculate lon and lat for the rectangle:
set lon1 = mylon - dist / abs(cos(radians(mylat)) * 69);
set lon2 = mylon + dist / abs(cos(radians(mylat)) * 69);
set lat1 = mylat - (dist / 69);
set lat2 = mylat + (dist / 69);

-- run the query:
SELECT destination.*,
3956 * 2 * ASIN(SQRT(POWER(SIN((orig.lat - dest.lat) * pi()/180 / 2), 2) +
COS(orig.lat * pi()/180) * COS(dest.lat * pi()/180) * POWER(SIN((orig.lon -dest.lon) * pi()/180 / 2), 2) )) as distance FROM locations destination, locations origin WHERE origin.location_id=_location_id
and destination.longitude between lon1 and lon2 and destination.latitude between lat1 and lat2 
having distance < dist ORDER BY Distance limit 10;
END

问题在于我不知道orig.lat是什么,他在哪里接过它,也许我错过了一些东西。你能指出我的错误或建议我在MySql中进行地理搜索的良好来源

1 个答案:

答案 0 :(得分:1)

这里构建的SELECT查询是找到一个点(在10个单位内)到点的位置 - 让我们称之为x

orig是x点,然后对于数据库中的每条记录,您要与orig latlon值进行比较,看它是否足够接近负荷。

在实践中,假设您正在为用户加载附近的夜总会列表,orig可能由用户输入其邮政编码或类似内容来确定...

如果您仍然感到困惑,请告诉我,我可以尝试提供更多帮助。