SitePoint Sponsor

# Thread: zip radius class return distance calculation also?

1. ## zip radius class return distance calculation also?

We use the following: http://www.phpclasses.org/browse/package/522.html

However, it doesnt return the distance that the zip is from the input zip code.

It uses this calculation/query:
\$query="SELECT zipcode FROM pmd_zipdata WHERE (POW((69.1*(lon-\"\$lon\")*cos(\$lat/57.3)),\"2\")+POW((69.1*(lat-\"\$lat\")),\"2\"))<(\$radius*\$radius) "

Anyway to get it to return also the distance?

2. methinks their distance calc is incorrect (could be wrong), here's mine:

Code:
`SQRT (((69.1*(latitude-{\$from_latitude})) ^ 2) + ((53*(longitude-{\$from_longitude})) ^ 2))`

3. Ok but do you know how you might return the distance also?

4. Also in yours where are you suppose to enter their entered radius (from the input)?

5. I used this to do a search from one zipcode to another (using geological locations) I had a large database filled with zipcodes as well as there city, state, lon and lat.

zipcodes is the listing of zipcodes and there city state and other things, and the locations is a database of locations for
business and such.

Edit: I believe this is the US zipcode listing that I used. http://www.zip-codes.com/

Code:
`SELECT z.zipcode, z.lon, z.lat, l.name, l.id, l.address, l.city, l.state, l.zip FROM zipcodes = z, locations = l WHERE(POW((69.1 * (z.lon - '\$lon') * cos(\$lat / 57.3)), 2) + POW((69.1 * (z.lat - '\$lat')), 2)) < (\$miles * \$miles) GROUP BY name`

6. Originally Posted by atDev
Ok but do you know how you might return the distance also?
Sure do, you also use this in the select:

PHP Code:
``` SQRT (((69.1*(latitude-{\$from_latitude})) ^ 2) + ((53*(longitude-{\$from_longitude})) ^ 2)) AS \"calc\""  ```
My PHP fields are in the {}

and as far as how far to search you add this to the where clause:

PHP Code:
``` AND SQRT (((69.1*(latitude-{\$from_latitude})) ^ 2) + ((53*(longitude-{\$from_longitude})) ^ 2)) <={\$miles_to_use}"  ```

7. Do you know how this performs on large sets of data?

8. Do you know how you can easily change this between miles and kilometers?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•