开发者

show restaurants within 100 miles

I have a master zip code table tbl_zipcode with all latitude and longitude values. Also a restaurant table with restaurant开发者_如何学Python list with zip codes.

Now I want to show the restaurants within 100 miles from user location i.e. from session user. How can I write a function which returns all the restaurants within 100 miles?

Any help is appreciated!!

Thanks.


So basically you have the longitude and latitude of the user and of all the restaurants..

check haversine formula this is helpful: http://www.movable-type.co.uk/scripts/latlong.html

To implement the formula is pretty simple


Determine all of the ZIP codes in a 100 mile radius from where you want to start (which is the current ZIP code based on the position of the session user), and include all Restaurants with those ZIP codes.


You need to device a way to calculate the distance between the user to those zip code's lat long.

Source: http://www.movable-type.co.uk/scripts/latlong.html


First thing would be to get the lat/long values of the current user's zip code.

SELECT latitude,longitude FROM tbl_zipcode WHERE zipcode = <USER_ZIPCODE>

Then you can use those values to calculate the distance for each coordinates pair in the database, e.g. by using this script (written in PHP so no time wasted thinking about Java functions).

But you might want to think about caching since it's a pretty heavy operation to do for each coordinates pair. Maybe you can precalculate those values and update the database each time a new restaurant comes in or so.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜