The Little Guy Posted February 28, 2011 Share Posted February 28, 2011 I have this to calculate the distance it is from once zipcode to another: SET @lat1 = '-093.076163'; SET @long1 = '+44.661216'; SELECT 96.1 * (180/pi()) * acos( sin(radians(@lat1)) * sin(radians(lat)) + cos(radians(@lat1)) * cos(radians(lat)) * cos(radians(log - @long1)) ) as dist, zip, city from zipcodes having dist <= 15 order by dist limit 10000; above code found here: http://www.zipcodeworld.com/docs/distance.pdf Currently it only seems to calculate the distance of a zipcode in the distance it is east / west of the current zipcode For example I am using my current town (Rosemount, MN) and it says the closest town to me is St. Paul, MN. But if you look at Google it is about 15 miles (by air). Now if I go west and draw a line straight down from St. Paul, then it takes about what the above code calculates. any suggestions to calculate longitude as well, or any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/229086-zip-codes-within-a-radius/ Share on other sites More sharing options...
fenway Posted March 1, 2011 Share Posted March 1, 2011 Don't use trig functions to calculate distance -- that's just wasteful. Latitude length is roughly proportional to degrees, while longitude is proportional to the cos (degrees); and a lookup table for every 5 degrees will suffice. Quote Link to comment https://forums.phpfreaks.com/topic/229086-zip-codes-within-a-radius/#findComment-1181057 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.