开发者

Using views to find list of users nearby using location in arguments

I am using drupal 6.X I am creating an api through views and views data开发者_运维技巧source module in which i will add the latitude and longitude of the location in the end of the url as we define the arguments in the view so that i can get the list of users in the order of the nearest first.


I think it would be easier to create a custom module. Basically you want to run the Haversine formula against your longitude and latitudes:

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance 
FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;

This is from http://code.google.com/apis/maps/articles/phpsqlsearch.html, and does this:

will find the closest 20 locations that are within a radius of 25 miles to the 37, -122 coordinate. It calculates the distance based on the latitude/longitude of that row and the target latitude/longitude, and then asks for only rows where the distance value is less than 25, orders the whole query by distance, and limits it to 20 results. To search by kilometers instead of miles, replace 3959 with 6371.

I've tried this exact forumla with MySQL and it works very well, I even have a fully fledged module working for it but unfortunately it's for Drupal 7 and relies entirely on the entity system so wouldn't be helpful in your case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜