Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Radius Search: What's closest to me? #5

Open
fireproofsocks opened this issue Mar 2, 2013 · 0 comments
Open

Radius Search: What's closest to me? #5

fireproofsocks opened this issue Mar 2, 2013 · 0 comments
Assignees

Comments

@fireproofsocks
Copy link
Member

A variation on the Gmarker snippet:

Include a &radius parameter that will filter the viable resources specified by the other parameters (i.e. the &parents or &resources).

E.g.

&radius=`5`
&radius=`5mi`
&radius=`8km`

OR

&radius=5 &units=miles

Related: add a system setting specifying the unit (miles or kilometers).

Search for "Great Circle" calculations for some inspiration:

http://stackoverflow.com/questions/574691/mysql-great-circle-distance-haversine-formula
http://stackoverflow.com/questions/1727137/sql-query-for-performing-radius-search-based-on-latitude-longitude

Another solution for this problem is to use a square instead -- this makes it a bit easier on the maths involved because you simply use a greater-than / less-than comparison on the lat/lng coordinates.

This should also set a placeholder for "results_msg" or "no_results" where we can say "There are X results within Y miles of you!" or "There were no results".

A related search is "Find the closest to me" -- check out this braindump powerpoint!
http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL

Also http://www.sphinxsearch.com/

Sample query:

CREATE PROCEDURE geodist (IN userid int, IN dist int)BEGINdeclare 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 users5where id=userid limit 1;
-- 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) )) asdistance FROM users destination, users originWHERE origin.id=userid
and destination.longitude between lon1 and lon2 and destination.latitude between lat1 and lat2 
having distance < dist ORDER BY Distance limit 10;END $$

Really, this would need to be a variation on the resourceQuery Snippets.

@ghost ghost assigned fireproofsocks Mar 2, 2013
@fireproofsocks fireproofsocks added this to the v3 : Static Maps milestone Feb 25, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant