Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Zip code radius search

Status
Not open for further replies.
Aug 23, 2004
174
US
I want to be able to search for items within a certain radius of the entered zip code. Users will be able to enter a zip code, select the radius, and results within that radius will be returned. I will be using a mysql database. Any recommendations?

Joe
Web Developer
 
doesn't google local do something like that?
 
Yes, google has an API you can use. I believe it's free as long as you don't query it over 15k times per day.

Download the google API and apply for a key.

Here's some code to get you started.

Code:
$map = new GoogleMapAPI('map');
$map->setAPIKey('their_key');

$startll = $map->geoGetCoords('26003');
$endll = $map->geoGetCoords('90210');

$distance = $map->geoGetDistance($startll['lat'],$startll['lon'],$endll['lat'],$endll['lon'],'M');

echo $distance;

or to skip the API once you have the lat and lon, here's how to calculate the distance yourself.

Code:
$distance = 3963.0 * acos(sin($lat1/57.2958) * sin($lat2/57.2958) + cos($lat1/57.2958) * cos($lat2/57.2958) *  cos($lon2/57.2958 - $lon1/57.2958));

echo $distance;

Have fun!

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top