[英]Geo search mysql
我一直在跟隨這個示例在MySQL中進行地理位置搜索(非常棒的教程!)。
這是我想出的:
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
是什么,他從哪里orig.lat
,也許我錯過了一些東西。 您能指出我的錯誤還是建議我在MySql中進行Geo搜索嗎?
此處正在構建的SELECT查詢旨在查找距離點很近(在10個單位內)的位置-我們稱其為x
orig
是點x,那么對於數據庫中的每條記錄,您都將與orig
的lat
和lon
值進行比較,以查看其是否足夠接近以加載。
實際上,假設您正在為用戶加載附近的夜總會列表, orig
可能由用戶鍵入他們的郵政編碼或類似的內容來確定。
如果您仍然感到困惑,請告訴我,我可以嘗試提供更多幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.