Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

php - Finding Zip Codes in a Specific Radius

I have found this popular PHP/MySQL Script called Zip Location by SaniSoft and it works great besides one thing: It doesn't in some instances.

It seems that any radius under 20 miles returns the same amount of zip codes as 20 miles. I have searched all over google, but to no avail and I was wondering if someone had some insight on this situation.

I would rather figure out this problem before having to pay for a program, and I could also use the learning experience. The database is a list of zip codes and longitudes and latitudes of each zip code. The script uses a method that determines the distance around the zip code entered and returns the zip codes in that radius based on their lon/lat.

Thank you!!

Edit: From using the distance function that the script provides I have discovered that the distance between the Zip Codes that the program gives me and my zip code are coming up as 0 miles.

MAJOR UPDATE

From research it turns out that the database has duplicate lat/lon values. Please be aware of this when using Zip Locator. Although the PHP does its job, you will need to find a new Database of zip codes. I will post my findings at a later date.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The following approximate distance calculations are relatively simple, but can produce distance errors of 10% of more. These approximate calculations are performed using latitude and longitude values in degrees. The first approximation requires only simple math functions:

Approximate distance in miles = sqrt(x * x + y * y)

where
      x = 69.1 * (lat2 - lat1)
and   
      y = 53 * (lon2 - lon1)

You can improve the accuracy of this approximate distance calculation by adding the cosine math function:

Approximate distance in miles = sqrt(x * x + y * y)

where
      x = 69.1 * (lat2 - lat1)
and   
      y = 69.1 * (lon2 - lon1) * cos(lat1/57.3)

If you need greater accuracy, you must use the exact distance calculation. The exact distance calculation requires use of spherical geometry, since the Earth is a sphere. The exact distance calculation also requires a high level of floating point mathematical accuracy - about 15 digits of accuracy (sometimes called "double-precision"). In addition, the trig math functions used in the exact calculation require conversion of the latitude and longitude values from degrees to radians. To convert latitude or longitude from degrees to radians, divide the latitude and longitude values in this database by 180/pi, or 57.2958. The radius of the Earth is assumed to be 6,371 kilometers, or 3,958.75 miles.

You must include the degrees-to-radians conversion in the calculation. Substituting degrees for radians, the calculation is:

          Exact distance in miles = 3958.75 * arccos[sin(lat1/57.2958) *
                               sin(lat2/57.2958) + 
                               cos(lat1/57.2958) * 
                               cos(lat2/57.2958) * 
                               cos(lon2/57.2958 - lon1/57.2958)]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...