Finding the points around a given radius of a specified point (haversine formula q)
I have a small problem,
I was just testing the use of the haversine formula for my project In the project I specify a point and I want to find all of the points within a given radius of the point that I provided. After much googling I tried this haversine formula query out.
SELECT service_id, ( 3959 * acos( cos( radians(51.500152) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-0.126236) ) + sin( radian开发者_StackOverflow社区s(51.500152) ) * sin( radians( latitude ) ) ) ) AS distance
FROM sys_t_taxi_real_time_servce HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;
I got the latitude and longitude for London and then specified data as shown in the order of (service_id, driver_id, journey_id, longitude, latitude, driver_status)
4, 1, , 51.5034, -0.174751, ''
5, 1, 2, 51.477106, -0.137329, ''
So for the result set however I get nothing, no rows returned
HOWEVER, when I remove the having clause I get the following result set (service_id, distance)
5, 4655.154600
4, 4658.309966
I am reqally stuck on this problem and is unable to move forward because of it and any help would be greatly appreciated.
The query and such was obtained from the following link http://code.google.com/apis/maps/articles/phpsqlsearch.html
Additionally does anyone know any other method to solve the same problem, I saw mysql spacial related stuff, but picked this as it was easier to use
Regards, MilindaD
I haven't used the formula you described but maybe another approach solves your problem: As you brought up a SQL query, I suppose you are using at least MySQL. Did you try to use the spatial functionality of the database? There are functions called CONTAINS and WITHIN. So you could simply create a buffer polygon around your coordinate from scratch and include the CONTAINS function in the where clause.
If you are not limited to MySQL, PostgreSQL, SQL Server or Oracle would give you a lot more. PostgreSQL is Open Source and the other ones are free to use for educational purposes (if I remember right).
Regards, Martin
精彩评论